簡體   English   中英

VBA需要幫助來識別與啟用了宏的按鈕相關聯的活動單元格

[英]VBA Need help identifying the active cell associated with macro enabled button

我正在嘗試創建一個計數器,該計數器顯示單擊啟用宏的按鈕的次數。 我在識別與啟用宏的按鈕關聯的活動單元時遇到了麻煩。 從單元格A9到A50,它將在任何位置(我將在頁面上使用多個宏按鈕)。
代碼需要直接在活動單元格的右邊查看單元格,並在按下按鈕時將1加到該數字上。 我是VBA的新手,沒有成功將建議的問題應用於我的方案。

如果此按鈕有助於解決問題,請按以下方法將其放置在紙張上。

Sub VBA_Input_Idea_inputbox()

  Dim MyInp As String
  Dim NextRow As Long
  Dim Counter As Long
  Dim btn As Button
  Dim t As Range


 Counter = Counter + 1

 MyInp = VBA.Interaction.InputBox("Please input idea", "LEARNING REQUEST")
   If MyInp = "" Then Exit Sub
 NextRow = Cells(Rows.Count, 3).End(xlUp).row + 1
 Range("C" & NextRow).Value = Excel.WorksheetFunction.Proper(MyInp)
 Set t = ActiveSheet.Range("A" & NextRow)
  Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
 .OnAction = "VBA_Love_It_msgbox3"
 .Caption = "Vote for this Idea!"
 .Name = "btn" & "Love This"
End With
  Range("B" & NextRow) = Counter

End Sub

在按鈕所附的宏中, Application.Caller將為您提供調用代碼的按鈕/形狀的名稱:

Sub VBA_Love_It_msgbox3()
    With ActiveSheet.Shapes(Application.Caller).TopLeftCell.Offset(0, 1)
        .Value = .Value + 1
    End With
End Sub

編輯:為此,您需要確保您的按鈕全部具有不同的名稱。

暫無
暫無

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

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