I'm creating a workbook in excel and using activeX checkboxes to hide/unhide some rows each time as a way to enable or disable them. For that I use VBA code. However in that code I specify a static range of rows. The problem is that if I insert a new row, everything is now offset and i have to manually rewrite all the intervals. Is there a way to do this dynamically.
Here is the code I use for two consecutive checkboxes:
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
[24:41].EntireRow.Hidden = False
Else: [24:41].EntireRow.Hidden = True
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2 = True Then
[42:49].EntireRow.Hidden = False
Else: [42:49].EntireRow.Hidden = True
End If
End Sub
I have searched for a while before asking the question, and I apologise if there is an answer somewhere, I'm a total newbie to excel and VBA and I don't think I can adapt a solution that is remotely similar to the problem I'm facing. Thanks for your precious help in advance.
You could try naming your range
thisworkbook.names.add name:="mydinamicrange" ,ReferstoR1C1:="Sheet1!R24c1:R49C10"
Then , you may use it with something like this :
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden = False
Else: thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden= True
End If
Have a Column in which you write an X in each row you want to hide / unhide.
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = False
End If
Next c
Else
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = True
End If
Next c
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.