Macro to hide/show and enable/disable a push button based on a cell value

Hi there. New here.

So I’ve been searching this website on how to hide or show a button. And I came across this macro:

sub hidebutton

oSheet = ThisComponent.CurrentController.ActiveSheet
oButton = oSheet.DrawPage.Forms.getByIndex(0).getByName(“button1”)
oButton.EnableVisible = False 'Hide the button

end sub

I tried it on my spreadsheet to hide a button and it works. But honestly my macro knowledge are all based on what I found on this website. I searched on the internet and I mostly got answers for Excel. So I need a macro that hides/shows a button based on a cell value, and another one that enables/disables it based on a cell value.

For example:
If A1=1, then button is visible
If A1=0, then button is not visible

and
If A1=3, then button is enabled
If A1=2, then button is disabled

I need these two functions separately for my use cases.

Thank you very much in advance!

  1. What idea behind making a button enabled, but not visible?
  2. Do you actually want to use A1 with 4 different “flag values” controlling Enabled and Visible, leaving each property unchanged if not directly addressed?
    In this case the first “then part” of your explanation should read “button set visible, property Enabled unchanged” and the others respectively.
  3. “set” and “leave unchanged” aren’t values, but instructions. (This is not about programming, but about the real world.) In programming it means: We need an Event to trigger the action.
  4. For a spredsheet there are some standard “real-softworld-events” you can assign an action (“macro” call) to.
  5. To choose the appropriate one, you need to decide if the comparisons like A1=0 should be made after any recalculation of the sheet, or only if the content (that can be a formula; its result is not “content” in this sense) of the cell was changed. (There are less plausible alternatives.)
  6. A functionality depending on a previous state in any way, can be afflicted by a previously arisen error. You need to decide if you can accept this in your case. If not, you should describe the intended actions exclusively using “set”, but never “leave”.
  7. In known cases this is handled using binary “flags” in an additive way. In your case e.g:
    (0 → not viible, not enabled)
    1 → visible
    2 → enabled
    3 → visible and enabled (3 = 1 + 2)

If you actually consider to use “personalized macros” in the long run, that’s definitely not the right way. You may start reading the famous texts by Andrew Pitonyak (“My Main Dowloads” there)…

When I wrote this, I hadn’t yet read your commnet on the first answer.
I posted it nonetheless. It may be worth reading.

1 Like

HelloWorld_conditional_buttons.ods (16.7 KB)

2 Likes

please tell us how it should work separately if both use-cases are triggered by the very same Cell A1

Edit

here is a very simplfied Example howto trigger the State of Buttons by Sheetevent: Content changed
Here is the Python-code:

def button_state_on_cell_Value(event):
    """
    """
    if event.AbsoluteName.endswith('$A$1'):
        sheet = event.Spreadsheet
        #first button
        control = sheet.DrawPage[0].Control
        control.Enabled = event.Value != 0.0
        # next button
        control2 = sheet.DrawPage[1].Control
        control2.EnableVisible = event.Value==3.0

and the Doc with embedded Script:
Button_state_on_Content_has_changed.ods (13.9 KB)

1 Like

Hi sorry for the confusion. I mean these two functions are not in the same macro/sub. Just consider I’m going to use each of these two functions in two different spreadsheets, not connected to another. So one spreadsheet has a macro for hiding/showing a button, the other spreadsheet has a macro for enabling/disabling a button. Thank you very much!