简体   繁体   English

VBA Excel按钮宏错误

[英]VBA Excel Button Macro Error

I am programmatically placing a button on a worksheet and it places fine, however when I click it i get an error saying "Cannot run the macro. The macro may not be available in this workbook or all macros may be disabled". 我以编程方式在工作表上放置了一个按钮,它的位置很好,但是当我单击它时,我收到一条错误消息:“无法运行该宏。此工作簿中的宏可能不可用,或者可能禁用了所有宏”。 I believe I've set it up fine but here is my code if anyone spots anything would greatly appreciate it. 我相信我已经把它设置好了,但是如果有人发现任何东西,我会在这里提供极大的赞赏。

Sub ButtonGenerator()

    Application.ScreenUpdating = False

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets("CRC")

    Dim lcolumncrc As Long
    lcolumncrc = CRC.LastColumnInCRC

    'Button Declarations
    Dim ShowHideDates As Button

    wsCRC.Buttons.Delete

    'Show/Hide Dates Button Set Up
    Dim SHDrange As Range

    Set SHDrange = wsCRC.Range(Cells(5, lcolumncrc + 2), Cells(5, lcolumncrc + 4))
    Set ShowHideDates = wsCRC.Buttons.Add(SHDrange.Left, SHDrange.Top, SHDrange.Width, SHDrange.Height)

    With ShowHideDates
        .OnAction = "wsCRC.SHDbtn"
        .Caption = "Show Hidden Date Columns"
        .Name = "ShowHideDates"
    End With

    Application.ScreenUpdating = True

End Sub

Sub SHDbtn()

    Dim wsCRC As Worksheet
    Set wsCRC = Worksheets("CRC")
    Dim ShowHideDates As Button

    Dim CurrentDateColumn As Long
    CurrentDateColumn = GetTodaysDateColumn()

    ActiveSheet.Unprotect

    If ShowHideDates.Caption = "Hide Old Date Columns" Then
        wsCRC.Range(wsCRC.Cells(5, 10), wsCRC.Cells(5, CurrentDateColumn - 6)).EntireColumn.Hidden = True
        ShowHideDates.Caption = "Show Hidden Date Columns"
    Else
        wsCRC.Range(wsCRC.Cells(5, 10), wsCRC.Cells(5, CurrentDateColumn - 6)).EntireColumn.Hidden = False
        ShowHideDates.Caption = "Hide Old Date Columns"
    End If

    ActiveSheet.Protect

End Sub

You're referring to a worksheet by the label you've given it within your code, not as a sheet itself. 您指的是在代码中给工作表指定的标签,而不是工作表本身。

Try changing: 尝试更改:

.OnAction = "wsCRC.SHDbtn"

to

.OnAction = "CRC.SHDbtn"

or even 甚至

.OnAction = "SHDbtn"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM