VBA Macro that Shows and Hides Rows depending on User Input

Sangdrax

New Member
Joined
Oct 13, 2014
Messages
9
Good Morning All,

I'm trying to write a macro that shows and hides rows on the click of a button.

I have rows 33 - 51 - of which certain groupings need to show when a specific user input is detected.

I was trying to make so that the selection was converted to a number (1/2/3/0) with formulas in the sheet (reference cell P1) and then the macro would show or hide rows depending on that.

I have pasted my current progress below: (Note - I'm brand new at this so it may be completely wrong)

Private Sub CheckBox2_Click()
If (Range("P1").Value) = 1 Then
Rows("33:41").Select
End If
If Selection.EntireRow.Hidden = True Then
Selection.EntireRow.Hidden = False
End If
If (Range("P1").Value) = 2 Then
Rows("42:51").Select
End If
If Selection.EntireRow.Hidden = True Then
Selection.EntireRow.Hidden = False
Else
Selection.EntireRow.Hidden = True
End If
If (Range("P1").Value) = 3 Then
Rows("33:36").Select
End If
If Selection.EntireRow.Hidden = True Then
Selection.EntireRow.Hidden = False
Else
Selection.EntireRow.Hidden = True
End If
If (Range("P1").Value) = 0 Then
Rows("33:36").Select
End If
If Selection.EntireRow.Hidden = True Then
Selection.EntireRow.Hidden = False
Else
Selection.EntireRow.Hidden = True
End If
Range("A1").Select
End Sub

Currently though doing it this way doesn't seem to work - I have other macros with this setup in my sheet but they are much more simple.

Any assistance would be greatly appreciated.

Thanks in advance!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Code:
Sub myMacro()
Rows("1:99999").Hidden = FALSE
If Range("P1").Value = 1 Then
Rows("33:41").Hidden = TRUE
End If
If Range("P1").Value = 2 Then
Rows("42:51").Hidden = TRUE
End If
If Range("P1").Value = 3 OR_
     Range("P1").Value = 0 Then
Rows("33:36").Hidden = TRUE
End If

End Sub
What this code does is first unhide all the rows. Then it will determine if the criteria in cell P1 is 0, 1, 2, or 3. Then it will hide the rows determined by the if statement.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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