excel - 每张纸上同一单元格中的按钮

标签 excel vba

我需要在返回主工作表的每张工作表的左上角 (A1) 放置一个按钮。我无法将它添加到所有工作表上 - 它在第一张工作表之后停止。

下面是我想出的。

Sub addhome()
    Dim btn As Button
    Dim t As Range


    For Each sh In Worksheets

        Set t = sh.Range("A1")
        Set btn = sh.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
        With btn
          .OnAction = "GotoHome"
          .Caption = "Home"
          .Name = "Home"
        End With    

    Next sh

    MsgBox ("done")
End Sub

当我打开一个新工作簿并制作一些虚拟工作表时,它可以工作,但是当我尝试在现有工作簿上执行此操作时,它只会在第一张工作表上添加一个按钮。

最佳答案

我找到了解决这种情况的方法 - 希望它会在 future 对某人有所帮助..

Sub addhome()

Dim btn As Button
Dim t As Range
Dim sh As Worksheet

For Each sh In Worksheets

    Debug.Print sh.Name
If sh.Name = "All" Then

Else

    Set t = sh.Range("A1")
    Set btn = sh.Buttons.Add(t.Left, t.Top, t.Width, t.Height)


    With btn
      .OnAction = "GotoHome"
      .Caption = "Home"
      .Name = "Home"
    End With
End If

Next sh



MsgBox ("done")

End Sub

我只是使用 IF-THEN 来避免问题表并击中所有其他问题表。希望我昨天能想到这一点。我知道复制有更简单的方法,但我喜欢找到最普遍准确的方法的挑战。我认为这很接近它。感谢你的帮助。

关于excel - 每张纸上同一单元格中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53962342/

相关文章:

excel - 出错跳到下一个

vba - Excel - VBA 检查工作表是否不 protected

excel - VBA 列的最后一行

vba - 如何让 Excel VBA 在 If and Then 之后停止

excel - 将 ActiveCell.Address 读入 Outlook

excel - 将 Google 街景图像嵌入 Excel

excel - 公式中的动态列索引名称错误

vba - 单元格似乎是空的但不是的奇怪情况

html - 我们可以在 VBA 中通过 css 路径访问元素吗?

excel - 将 Excel 工作表另存为 .txt