在导航工作表上为所有其他工作表创建命令按钮

Creating command button on Navigation worksheet for all other worksheets

当前代码:

Sub CreateFormsButton()

Dim ws As Worksheet
Dim btn As Button
Dim rng As Range
Dim counter As String
counter = 6

For Each ws In ThisWorkbook.Worksheets
     If ws.Name = "Navigation" Or ws.Name = "Todo" Then
     Else
     With ws
        Set rng = ThisWorkbook.Sheets("Navigation").Range("C" + CStr(counter))
        Set btn = .Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height * 2)
     With btn
        .Caption = ws.Name
        .OnAction = "GoToWS"
        .Font.Bold = True
        .Font.Size = 16
     End With
     End With
     End If
     counter = counter + 2
Next ws

End Sub

代码按原样工作,但是,按钮被放置在工作表本身而不是预期的导航工作表上。我设置 Set rng = ThisWorkbook.Sheets("Navigation").Range("C" + CStr(counter)) 是不是做错了什么?这似乎对按钮的放置位置没有任何影响。感谢您的帮助。

您需要在导航的 Buttons 集合中添加按钮 Sheet。

Set btn = ThisWorkbook.Sheets("Navigation").Buttons.Add(rng.Left, rng.Top, rng.Width, rng.Height * 2)