VBA: Hide/Unhide Button - Cell Value - Some Rows to Remain Hidden Always

Ali Atwi

New Member
Joined
Mar 21, 2013
Messages
31
Excel 2010:

So I have VBA script that toggles between hiding and unhiding a set of rows.

Private Sub CommandButton1_Click()
Dim myRng As Range
Dim myRng2 As Range
Dim IsHidden As Boolean


Set myRng = Me.Range("ProductionData")
IsHidden = myRng(1).EntireRow.Hidden
Me.Range("ProductionData").EntireRow.Hidden = True
myRng.EntireRow.Hidden = Not (IsHidden)


End Sub


However I have to add another criteria so that a set of rows are to remain hidden all the time.

Which rows are to remain hidden all the time is dependent on the cell value in column A within that range.

The cell values in column A is "Yes" or "No".

Any rows with cell value in column A of "No" are to remain hidden during the toggle.

How can I add or embed an if/and statement into the script so that the hide/unhide also looks for the cell value in column A and just toggles between the rest while any row with cell value column A of "No" remains hidden all the time?

I hope I was clear enough.

Hoping to be pointed in the right direction.

Thank you.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I found out that I can not show the arrow, however, will I still be capable of toggling the hide/unhide?
 
Upvote 0
I found out that I can not show the arrow, however, will I still be capable of toggling the hide/unhide?


I came up with this. it still gives me a small error though but works.

Private Sub CommandButton1_Click()


Dim myRng As Range
Dim IsHidden As Boolean


Set myRng = Me.Range("ProductionData")
IsHidden = myRng(1).EntireRow.Hidden
Me.Range("ProductionData").EntireRow.Hidden = True
myRng.EntireRow.Hidden = Not (IsHidden)


If myrange.EntireRow.Hidden = False Then


With ActiveSheet
.Range("A31:A186").AutoFilter
.Range("A31:A186").AutoFilter field:=1, Criteria1:="Yes"
End With
Else
End If




End Sub
 
Upvote 0
Alright I played around with it, I really dont understand what the hell I did, but it worked. Is there any shorter version of this, because all I did was trial and error with the lines, and if someone would care to explain the code for me I would really really appreciate it. (Especially the boolean part, still haven't wrapped my head around it). Thanks.

Private Sub CommandButton1_Click()


Dim myRng As Range
Dim IsHidden As Boolean


Set myRng = Me.Range("ProductionData")
IsHidden = myRng(1).EntireRow.Hidden
Me.Range("ProductionData").EntireRow.Hidden = True
myRng.EntireRow.Hidden = Not (IsHidden)


If Me.Range("ProductionData").EntireRow.Hidden = False Then
With ActiveSheet
.Range("A31:A186").AutoFilter
.Range("A31:A186").AutoFilter field:=1, Criteria1:="Yes"
End With
End If




End Sub
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,293
Members
449,218
Latest member
Excel Master

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