2017-06-02 904 views
0

我的问题是,我只需要在标记的单元格上执行一个宏。Excel VBA,在选定的单元格上执行宏

宏需要做到以下几点:

选择的小区例如为20 * 20 * 20总是3个数字总是格式化。

它应该复制此文本在数字前面添加一个“=”并将其输出到另一列。

守则我到现在为止是:

Sub First() 
' 
' First Makro 
' 

' 
    Selection.Copy 
    Range("G11").Select 
    ActiveSheet.Paste 
    Application.CutCopyMode = False 
    ActiveCell.FormulaR1C1 = "=20*20*20" 
    Range("G12").Select 
End Sub 

我有这个代码与记录微距功能

非常感谢

+0

你在哪里复制,你在哪里粘贴?你的问题不清楚。请准确解释你想达到什么目的? –

+0

@SiddharthRout嗨Sir 是的我希望能够手动选择手机,它被复制到同一行,但进一步2列 – Valentino

+0

所以你是从上述代码中的“E11”复制? –

回答

1

@SiddharthRout准确,但我必须能够手动选择它,因为有时例如E17有时是e33,输出总是需要是G列在同一行

这是你正在尝试的?

Sub Sample() 
    Dim wb As Workbook 
    Dim ws As Worksheet 

    Set wb = ThisWorkbook 
    '~~> Replace Sheet1 with the relevant sheet name 
    Set ws = wb.Sheets("Sheet1") 

    '~~> Check if what the user selected is a valid range 
    If TypeName(Selection) <> "Range" Then 
     MsgBox "Select a range first." 
     Exit Sub 
    End If 

    '~~> Check if the user has selected a single cell 
    If Selection.Cells.Count > 1 Then 
     MsgBox "Please select a single cell" 
     Exit Sub 
    End If 

    ws.Range("G" & Selection.Row).Formula = "=" & Selection.Value 
End Sub 
+0

我在执行此脚本时遇到运行时错误 – Valentino

+0

您可能必须刷新页面才能看到编辑 –

+0

感谢您的帮助,但执行宏时出现此错误。 运行时错误9和束缚了阵列的 http://imgur.com/a/37f39 – Valentino