Loop Through Visible Columns Only with Userform

jewkes6000

Board Regular
Joined
Mar 25, 2020
Messages
60
Office Version
  1. 365
Platform
  1. Windows
I have the following userform code which loops through columns and hides certain columns based on some criteria; however, if the rows are already hidden, when the toggle button is turned off, it shows the hidden columns which are never supposed to be visible. How do I get the code to loop through visible columns only? Thank you in advance for any help.

VBA Code:
Private Sub LaborButton_Click()
    If Controls("LaborButton") = True Then
        Dim i As Long
              
        For i = 260 To 15 Step -1
            If Cells(3, i) = "Labor" Then Columns(i).Hidden = True
      
        Next i
        LaborButton.BackColor = vbRed
          
     Else
      
        For i = 260 To 15 Step -1
            If Cells(3, i) = "Labor" Then Columns(i).Hidden = False
        Next i
        LaborButton.BackColor = vbYellow
    End If
    ActiveWindow.ScrollColumn = 1
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
"it shows the hidden columns which are never supposed to be visible"
I don't see any logic beyond Cells(3, i) = "Labor" AND Controls("LaborButton") = True|False. What logic explains columns that are never supposed to be visible? You seem to be missing that necessary information. You might provide a list of column numbers to skip such as:

VBA Code:
Private Sub LaborButton_Click()
    Dim i As Long
          
    For i = 260 To 15 Step -1
        Select Case i
            Case 2, 6, 17, 22 To 45 'leave these columns alone
                'do nothing
            Case Else
                If Cells(3, i) = "Labor" Then Columns(i).Hidden = Controls("LaborButton")
        End Select
    Next i
    LaborButton.BackColor = IIf(Controls("LaborButton"), vbRed, vbYellow)
          
    ActiveWindow.ScrollColumn = 1
End Sub

Just curious. Why are you looping backwards?
 
Upvote 0
@dataluver - The reason why certain columns should always be hidden is because I have a template with about 250 columns. The idea is that when creating a worksheet from the template, you only choose the columns you need and the others get hidden (I hide them rather than delete them in the event they are needed later on). So there are a bunch of hidden columns before this code is even run. The purpose of this code is to take the columns which are part of the new worksheet and have the ability to turn on/turn off columns which you actually need. Hope this all makes sense.

So with your code suggestion and skipping certain columns, that would work if I could make the columns which are skipped dynamic. Right now, it's looping through row 3; however, rows 1 and 2 determine which rows are hidden. So if I could get it to skip columns which have the text "HIDE" in rows 1 or 2, then it would be skipping the previously hidden rows. Note that my macro which creates the worksheet from the original template loops through row 1 & 2; (Cells ( 1,i) and (Cells 2,i), and hides columns which have the text "HIDE" in either of these rows.

No particular reason for looping backwards. I'm new to VBA and it was suggested to me here on Mr. Excel.
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,973
Members
449,059
Latest member
oculus

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