Excel VBA,在选中的单元格上执行macros

我的问题是,我只需要在标记的单元格上执行一个macros。

macros需要做到以下几点:

选定的单元格始终格式化,例如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 

我已经得到这个代码与loggingmacrosfunction

非常感谢

@SiddharthRout正好,但我需要能够手动select它,因为有时它是例如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