简体   繁体   中英

Excel | Disable button at start

I am working on an excel file that will work as a calendar with specifications. I want to have a button at each day. Since I want this to be reusable for other years, I will have buttons on columns with no days (for example, if January starts on a tewsday, Monday will have a button, but nothing on the day, since it is from December).

在此处输入图像描述

I know it is possible to set a button enable = False, but I don't know where to put that code. I don't want it to be disabled when another button is clicked but at the opening of the file. I am new to vba, I'm sorry if this is something really simple.

My approach needs those cells with days from previous month to be empty or "", if theres any value inside it wont work (instead you change the logic to treat cells values like numbers instead of strings).

I noticed that days in your calendar are in string format or so (ie: "01") that's why I use Len() to evaluate length of string.

This code will set buttons visibility based on TopLeftCell value. Visible = True to days with some value, and Visible = False to empty values.

There is a way to make a button "Enable" but that property is for buttons inside an UserForm.

Tell me if it works for your case, since Sheet.CurrentRegion may cause some issues if your cells are way to much separate from each other, plus it could also hide some other buttons you have. If any of those scenarios do happen let me know, I'll continue helping you anyways!

Sub Set_Buttons_Visibility()

  Dim Sheet As Worksheet
  Dim Rng_ As Range
  Dim Shape As Shape


    Set Sheet = ActiveSheet                         'Set Sheet
    Set Rng_ = Sheet.Cells(1, 1).CurrentRegion      'Set current region
    'Rng_.Select

    'Hide buttons from previous month
    For Each Shape In Sheet.Shapes
        strTemp = CStr(Shape.TopLeftCell)           'Get Button day, as string
        bShow = (Len(strTemp) > 0)                  'Check if strTemp is empty
        Shape.Visible = bShow                       'Hide button
        ''''Shape.delete                            'Delete shape (only if you have another procedure to recreate the buttons)
    Next
    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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