+ Reply to Thread
Results 1 to 18 of 18

Greying out buttons based on cell contents

  1. #1
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Greying out buttons based on cell contents

    Hi,
    Complete newbie to VBA stuff, here is a question:
    I have a spreadsheet with values in the certain cells and several buttons which carry out various functions however I would like the buttons to be only available if certain cells contain certain values. Therefore if a value isnt present i.e. empty in a certain cell (say A1) then I would like the button to be greyed out or visibly different than 'available' buttons (i.e. buttons that can be clicked because values are present in the cells required.
    Hope this makes sense, thanks

  2. #2
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: Greying out buttons based on cell contents

    Hi frogboy,

    Which kind of button are these.. is this ActiveX form button for Form controls ?
    I guess, a sample file would be more helpful. Thanks.


    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    DILIPandey, Excel rMVP
    +919810929744 (India), +971528225509 (Dubai), dilipandey@gmail.com

  3. #3
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    Just a form control button will suffice. cheers

  4. #4
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: Greying out buttons based on cell contents

    Hi frogboy,

    I wanted to know what type of buttons do you have in your spreadsheet. Are those Active X buttons or Form control buttons ? Thanks.

    Regards,
    DILIPandey

    <click on below 'star' if this helps>

  5. #5
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    form control

  6. #6
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: Greying out buttons based on cell contents

    Hi frogboy,

    It should be ActiveX form button, see the attached file and look into the comment in yellow highlighted cell A1. Thanks.

    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    Thanks DILIPandey,

    how do I make the the button 'not available' if the value in A1 is nothing i.e. null?

    I tried changing the 1 to a "Null" as well as Null but doesnt seem to work, what am I missing here?
    If Range("a1").Value = "Null" Then

    Also what does the following do in the VBA script? (sorry for my lack of knowldge on VBA!!)

    'Entry point for RibbonX button click
    Sub ShowATPDialog(control As IRibbonControl)
    Application.Run ("fDialog")
    End Sub

    'Callback for RibbonX button label
    Sub GetATPLabel(control As IRibbonControl, ByRef label)
    label = ThisWorkbook.Sheets("RES").Range("A10").Value
    End Sub
    cheers

  8. #8
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    ok just figured it out:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range("a1").Value = "" Then
    CommandButton1.Enabled = False
    Else
    CommandButton1.Enabled = True
    End If
    End Sub

  9. #9
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    next question if I want the range to be multiple cells how do i do this? I have tried e.g. (a1:a4) but this doesnt seem to work? Suggestions? cheers

  10. #10
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    ...also how about multiple blocks of cells e.g (a1:a4) and (d1:d4) etc
    (Apologies for the series of questions) cheers

  11. #11
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    Another question:

    The value in cell "a1" is derived from a Vlookup statement which in turn comes from a drop down list. Choosing a new item from the list updates the value in cell "a1" which should either make the button active or not active however this doesnt do it 'live' and immediately, it is only when I click in another cell that the button changes state. How do I get the button state to change immediately after selecting a field in the drop down list?
    cheers

  12. #12
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: Greying out buttons based on cell contents

    Hi Frogboy,

    For multiple cells or multiple blocks, you can use worksheet function in vba... something like if application.worksheetfunctions.counta(selection)>0 then enable command button. You need to decide if all the cells on any one of the cells are non blank(s).. also you need to give the references in place of selection as per your choice of ranges.. Thanks.

    Regards,
    DILIPandey

    <click on below 'star' if this helps>

  13. #13
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    not sure I understand!
    If for example there is ANY text in cells A1:A3 AND C1:C3 AND F4:F7 then I want the button to be available, if there is no text at all in these cells then the button should be disabled. Could you provide an example?

    Also as mentioned the values in the the cells can change (these are VLOOKUP cells) dependant upon what is selected via a drop down list, when the contents of the list is changed the cells update straight away but the button only updates whether it is available or not when I click on another cell, how can I ensure the button changes state immediately. Again any examples? Thanks

  14. #14
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: Greying out buttons based on cell contents

    Hi frogboy,

    I have incorporated your criteria into the attached file, review the same and let me know if this helps. Thanks.


    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    Attached Files Attached Files

  15. #15
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    Hi DILIPandey,

    I thought this was the one! Unfortunatley if the cells have an formulas in them then the code doesnt appear to see the cell as empty. The code works fine if text is filled manually into the cells however I have VLOOKUP statements in the cells therefore even when the cell reports back empty the code still seems to see the formula in the cell thus assumes it isnt empty. How can this be resolved? cheers

  16. #16
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: Greying out buttons based on cell contents

    Ok..

    so do you want the button to available if the cell is NOT blank BUT can have formulas with empty output?
    Can you share few examples to cover every loop hole here. thanks.

    Regards,
    DILIPandey

    <click on below 'star' if this helps>

  17. #17
    Registered User
    Join Date
    12-21-2011
    Location
    Aus
    MS-Off Ver
    Excel 2010
    Posts
    62

    Re: Greying out buttons based on cell contents

    attachedTest.xlsm

  18. #18
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: Greying out buttons based on cell contents

    Hi frogboy,

    See the attached file. Thanks

    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1