簡體   English   中英

Excel VBA,在選定的單元格上執行宏

[英]Excel VBA, execute Macro on selected cell

我的問題是我只需要在標記的單元格上執行宏。

宏需要執行以下操作:

所選單元格的格式始終為例如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

我已經通過記錄宏功能獲得了此代碼

非常感謝

確實是@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM