简体   繁体   中英

Excel VBA “unhide rows” changes the order of ranges while unhiding

I am using following macro to unhide rows step by step by pressing the button. However it occasionally changes the order of my ranges. So sometimes it unhides first "31:34" and sometimes first I see "43:46" or "39:42" or "35:38". Where can be the problem? Is it better to use different macro code if I have always 4 rows to unhide step by step? I have two cases when I am unhiding rows starting from the top and another case when I am starting to unhide rows from the bottom by clicking the button. Thats why I am using macro with ranges.

Sub UnhideEducation()
Static counter As Byte

    counter = (counter + 1) Mod 5

    Select Case counter
        Case 1
            Rows("31:34").EntireRow.Hidden = False
        Case 2
            Rows("35:38").EntireRow.Hidden = False
        Case 3
            Rows("39:42").EntireRow.Hidden = False
        Case 4
            Rows("43:46").EntireRow.Hidden = False
        Case 5
            Rows("43:46").EntireRow.Hidden = False
    End Select
End Sub

How about having something simpler like below and have a separate code for each button which will do hide/unhide not just hide !,

在此处输入图片说明

Sub Unhideyellow()
            Rows("34:40").EntireRow.Hidden = Not Rows("34:40").EntireRow.Hidden
End Sub
Sub UnhideGrey()
            Rows("41:46").EntireRow.Hidden = Not Rows("41:46").EntireRow.Hidden
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM