使用命令按钮更改多个单元格值

时间:2015-07-07 15:13:49

标签: excel vba button command

我试图找出如何将3个不同工作表上的3个单元格更改为与命令按钮标题相同的值。如果只有一个命令但我似乎无法使用它来处理多个命令,我设法让它工作。

Private Sub CmdSME100_Click()

    Worksheets("Calculator").Range("I1") = Me.CmdSME100.Caption
    Worksheets("Tariff Matrix").Range("A1") = Me.CmdSME100.Caption
    Worksheets("Bolt-On Matrix").Range("A1") = Me.CmdSME100.Caption

End Sub

现在看来这是我想要开始工作的代码。而且我似乎无法弄清楚为什么它不会在所有不同的纸张上工作。

我需要这个作为我要求代码更改然后触发自动过滤器的单元格。

任何建议都将不胜感激。

由于

3 个答案:

答案 0 :(得分:0)

设置工作表变量以访问它们

Private Sub CmdSME100_Click()
Dim ws As Excel.Worksheet

    Set ws = Application.Worksheets(1)
    ws.Range("I1") = Me.CmdSME100.Caption
    Set ws = Application.Worksheets(2)
    ws.Range("A1") = Me.CmdSME100.Caption
    Set ws = Application.Worksheets(3)
    ws.Range("A1") = Me.CmdSME100.Caption
End Sub

如果您的工作表在同一索引中不可靠,您可以按名称搜索它们。

代码看起来像这样。

'Find the worksheet named *BBCOV*
iFoundWorksheet = 0
For iIndex = 1 To Application.ActiveWorkbook.Worksheets.Count
    Set ws = Application.Worksheets(iIndex)
    If UCase(ws.Name) = "BBCOV-PURGED" Then
        iFoundWorksheet = iIndex
        Exit For
    End If
Next iIndex
If iFoundWorksheet = 0 Then
    MsgBox "No worksheet was found with the name BBCOV-PURGED (this is not case sensetive). Aborting."
    GoTo Abort
End If
Set ws = Application.Worksheets(iFoundWorksheet)
ws.Activate

答案 1 :(得分:0)

也许你可以试试"激活"床单。考虑"计算器"是Sheet1,"关税矩阵"是Sheet2和" Bolt-On Matrix"是表3;

Private Sub CmdSME100_Click()
Sheet1.Activate
Sheet1.Range("A1") = Me.CmdSME100.Caption
Sheet2.Activate
Sheet2.Range("A1") = Me.CmdSME100.Caption
Sheet3.Activate
Sheet3.Range("A1") = Me.CmdSME100.Caption
End Sub

答案 2 :(得分:0)

我认为你不能像那样抓住标题。

请改为尝试:

bool

因此...

ActiveSheet.Buttons(Application.Caller).Caption