Macros Box Opens When Running VBA Code

zobro93

New Member
Joined
Jul 4, 2017
Messages
5
Hi all,

I'm attempting to Hide Sheet titled "LOW" if B2 is "FALSE", or Unhide if B2 is "TRUE", however when I press 'Run Sub', the Macros Dialog Box pops up. I've attempted to find the answer elsewhere, but to no avail.

CODE AS FOLLOWS:
Private Sub Worksheet_Change(ByVal Target As Range)


If Range("B2").Value = "FALSE" Then Sheets("LOW").Visible = False
Else: If Range("B2").Value = "TRUE" Then Sheets("LOW").Visible = True

End If

End Sub

Thanks All :)
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi. There will be lots of ways to write this but heres one that should work for you:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

myVar = Range("B2").Value

If Not IsError(myVar) Then
    If myVar = False Or myVar = True Then
        Sheets("LOW").Visible = myVar
    End If
End If

End Sub
 
Upvote 0
Hi Steve,

Still doesn't work - I've saved it as macro enabled, but somehow still not actioning. Any ideas?

Cheers,

Z
 
Upvote 0
Where are you placing the code? You need to right click on the sheet tab. Press view code. Paste the code into the big white space. Close the vba window. Then type false into cell B2. The sheet named Low should be hidden. Type true into B2 and it reappears.
 
Upvote 0
I'm working on Mac & Excel 2011, so going into "Visual Basic Editor" from the Developer Tab, entering the code on the "Database" spreadsheet, where the B2 Cell is referencing to. When I right-click sheet tab, there is no "View Code" - not sure if this is due to Mac.
 
Upvote 0
Maybe im not sure. Cant help with that im afraid as im on a pc and never used a mac.
 
Upvote 0
The code you've posted will be triggered when a change is made on the worksheet, you wouldn't manually execute it via 'Run Sub' or whatever.

When you goto Run Sub and the cursor is not positioned within a sub that can be run manually the Macros dialog will pop up.
 
Last edited:
Upvote 0
Hi Norie, cursor is definitely within the code before I execute "run sub". Questioning if it's because I had placed it without "right-clicking sheet to View Code". When I close it all, and change B2 to False/True, nothing happens :(
 
Upvote 0
If you type TRUE/FALSE in a cell then Excel interpret it as a boolean value, in your code you are looking for the strings 'TRUE' or 'FALSE'.

Try changing the code to this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Range("B2").Value = False Then
        Sheets("LOW").Visible = False
    ElseIf Range("B2").Value = True Then
        Sheets("LOW").Visible = True
    End If

End Sub
 
Upvote 0
Thanks all for your assistance - Norie, this code worked. Not sure what happened with the others, investigating now. Cheers!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top