我需要在返回主工作表的每张工作表的左上角 (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/