如何在Excel 里用VBA代码为多个工作表中增加一个返回主页按键?

2025-01-06 11:10:26
推荐回答(2个)
回答1:

Public sub addButton()
Dim i As Integer
Dim n As Integer
Dim SheetName as String
n = ActiveWorkbook.Sheets.Count
For i = 1 To n
SheetName =ActiveWorkbook.Sheets(i).Name
if SheetName <> "主页界面" then
Call Worksheets(SheetName ).OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False , DisplayAsIcon:=False, Left:=108.75, Top:=1.5, Width:=110.25, Height _
:=40.5)
end if
Next
End sub
然后在每一页里面加上以下语句
Private Sub CommandButton1_Click()
Worksheets("主页界面").Activate
End Sub

回答2:

在没页加个按钮,按钮代码:
Private Sub CommandButton1_Click()
Worksheets("主页界面").Activate
End Sub