VBA to Hide/Unhide both rows and columns on same sheet

onthegreen03

Board Regular
Joined
Jun 30, 2016
Messages
148
Office Version
  1. 365
Platform
  1. Windows
Hi - I pieced together code that hides/unhides, using a checkbox, a set of columns on another sheet. Can someone tell me how I can modify this to also include an entire row on the same sheet? So in the example below hide/unhide columns C:N and row 29. Thank you for your help!

Private Sub CheckBox1_Click()
Dim xAddress As String
xAddress = "C:N"
If CheckBox1.Value Then
Application.Sheets("RHA CGF Model").Columns(xAddress).Hidden = True
CheckBox1.Caption = "Include"
Else
Application.ActiveSheet.Columns(xAddress).Hidden = False
CheckBox1.Caption = "Exclude"
End If
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
How about
VBA Code:
Private Sub CheckBox1_Click()
   With CheckBox1
      Sheets("RHA CGF Model").Columns("C:N").Hidden = .Value
      Sheets("RHA CGF Model").Rows(29).Hidden = .Value
      .Caption = IIf(.Value, "Include", "Exclude")
   End With
End Sub
 
Upvote 0
Solution
Fluff to the rescue as always. Simple and effective, thank you! If I wanted to include a series of rows would I just change it to this?

Private Sub CheckBox1_Click()
With CheckBox1
Sheets("RHA CGF Model").Columns("C:N").Hidden = .Value
Sheets("RHA CGF Model").Rows(29:30).Hidden = .Value
.Caption = IIf(.Value, "Include", "Exclude")
End With
End Sub
 
Upvote 0
As long as they are contiguous then yes, as long as you put the rows numbers in quotes like Rows("29:30")
 
Upvote 0
Thanks Fluff. Last question regarding this topic. What if I have a string of columns that I wanted to include. Would it be:

Private Sub CheckBox1_Click()
With CheckBox1
Sheets("RHA CGF Model").Columns("C:N,Y:AC,BA:BD").Hidden = .Value
Sheets("RHA CGF Model").Rows(29).Hidden = .Value
.Caption = IIf(.Value, "Include", "Exclude")
End With
End Sub
 
Upvote 0
Yes, and it didn't work. I tried it a few different ways and it is still stopping there in error.
 
Upvote 0
Thanks Fluff, that worked great! Sorry about the confusion and multiple questions.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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