Excel 2013 VBA为新工作簿创建按钮

时间:2015-03-05 15:18:21

标签: excel vba excel-vba

长时间用户,第一次海报!

我在校园食品架工作,最近我非常积极地参与简化他们的报告和数据输入。我目前正在执行的任务是创建一个程序,创建一个统一的表1,这样我到目前为止建立的报告程序可以很容易地导出到新的工作簿,很久我毕业。我熟悉(但绝不是专家)使用C / C ++,Python和最近的VBA。

我在为Sheet 1创建按钮时遇到错误。错误是:“无法识别光标下的标识符”。我正在寻找这个代码的结果是创建5个单独的按钮链接到已经定义的5个独立的子程序。我想把它们放在特定尺寸的特定地方。有问题的代码是:

Sub DONOTUSEbuttonMaker()
Dim Report1, Report2, Report3, Unique, NewWork As Object

Dim Targeter As Range
Dim i As Integer

For i = 1 To 5
  Select Case i:
    Case 1:
        Set Targeter = Worksheets(1).Range(Cells(3, 7), Cells(3, 7))
        Set Report1 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=2, Height:=0.33)
        With Report1
            .OnAction = "WeeklyReportsP1"
            .Caption = "Weekly Reports P1"
            .Name = "Weekly Reports P1"
        End With
    Case 2:
        Set Targeter = Worksheets(1).Range(Cells(5, 7), Cells(5, 7))
        Set Report1 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=2, Height:=0.33)
        With Report2
            .OnAction = "WeeklyReportsP2"
            .Caption = "Weekly Reports P2"
            .Name = "Weekly Reports P2"
        End With
    Case 3:
        Set Targeter = Worksheets(1).Range(Cells(7, 7), Cells(7, 7))
        Set Report1 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=2, Height:=0.33)
        With Report3
            .OnAction = "WeeklyReportsP3"
            .Caption = "Weekly Reports P3"
            .Name = "Weekly Reports P3"
        End With
    Case 4:
        Set Targeter = Worksheets(1).Range(Cells(9, 7), Cells(9, 7))
        Set Report1 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=2, Height:=0.33)
        With Unique
            .OnAction = "CalculateUnique"
            .Caption = "Calculate Unique"
            .Name = "Calculate Unique"
        End With
    Case 5:
        Set Targeter = Worksheets(1).Range(Cells(11, 7), Cells(11, 7))
        Set Report1 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=2, Height:=0.33)
        With NewWork
            .OnAction = "NewWeekWorkSheet"
            .Caption = "Create New Worksheet"
            .Name = "Create New Worksheet"
        End With
End Select
Next i


End Sub

错误在包含.OnAction的行中的情况2中生成。对我来说这似乎也很奇怪,因为它在案例1中没有引发错误......任何帮助都会非常感激!

2 个答案:

答案 0 :(得分:0)

在案例1复制和粘贴后,您似乎忘记将“Report1”更改为Report2,Report3等。(:

答案 1 :(得分:0)

感谢您提供的所有精彩反馈!它现在有效,这里是代码:

Sub DONOTUSEbuttonMaker()
Dim Report1, Report2, Report3, Unique, NewWork As Button
Dim Targeter As Range

Set Targeter = Worksheets(1).Cells(3, 7)
Set Report1 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=144, Height:=24)
With Report1
    .OnAction = "WeeklyReportsP1"
    .Caption = "Weekly Reports P1"
    .Name = "Weekly Reports P1"
End With

Set Targeter = Worksheets(1).Cells(5, 7)
Set Report2 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=144, Height:=24)
With Report2
    .OnAction = "WeeklyReportsP2"
    .Caption = "Weekly Reports P2"
    .Name = "Weekly Reports P2"
End With

Set Targeter = Worksheets(1).Cells(7, 7)
Set Report3 = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=144, Height:=24)
With Report3
    .OnAction = "WeeklyReportsP3"
    .Caption = "Weekly Reports P3"
    .Name = "Weekly Reports P3"
End With

Set Targeter = Worksheets(1).Cells(9, 7)
Set Unique = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=144, Height:=24)
With Unique
    .OnAction = "CalculateUnique"
    .Caption = "Calculate Unique"
    .Name = "Calculate Unique"
End With

Set Targeter = Worksheets(1).Cells(11, 7)
Set NewWork = Worksheets(1).Buttons.Add(Targeter.Left, Targeter.Top, Width:=144, Height:=24)
With NewWork
    .OnAction = "NewWeekWorkSheet"
    .Caption = "Create New Worksheet"
    .Name = "Create New Worksheet"
End With

End Sub

有关清理它的任何建议,将不胜感激!我是面向对象编程的新手,并希望得到一些关于更好的编程实践的反馈!再次感谢!