I need the same code to run on sheets 5 - 100 of my workbook

andrewlau2881

New Member
Joined
Oct 20, 2023
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have sheets which are updated from a master for hundreds of people in my organisation. When I update the master I would like all the other sheets to recognise this and unhide columns which have now been populated from the master. The VBA code I have works on the sheet that is open. However, I would like it to then run on sheet 5-100 of that same workbook. I have tried piecing together something from various sources but can't get anything to work fully.

VBA Code:
Sub Workbook_Open()

Dim p As Integer
For p = 2 To 31
If Cells(1, p).Value = "" Then
Columns(p).Hidden = True
Else: Columns(p).Hidden = False
End If
Next p

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
This should work
VBA Code:
Private Sub Workbook_Open()
    Dim i As Integer
    Dim ws As Worksheet
  
    Application.EnableEvents = False
    Application.ScreenUpdating = False
  
    For Each ws In ActiveWorkbook.Sheets
        For i = 2 To 31
            If ws.Cells(1, i).Value = "" Then
                ws.Columns(i).Hidden = True
            Else
                ws.Columns(i).Hidden = False
            End If
        Next
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
I would make the 2 to 31 a range, probably a sheet named range incase it changes but this should get you started
 
Upvote 0
I would like it to then run on sheet 5-100 of that same workbook.
Hi, welcome to the forum. If you are referring to the position of the sheets in the workbook, i.e. starting from the 5th tab along in the list of sheet tabs, then here is another option you can try.

VBA Code:
Sub Workbook_Open()
Dim p As Long, i As Long
For i = 5 To 100
    With ThisWorkbook.Sheets(i)
        For p = 2 To 31
            .Columns(p).Hidden = .Cells(1, p).Value = ""
        Next p
    End With
Next i
End Sub
 
Upvote 0
This should work
VBA Code:
Private Sub Workbook_Open()
    Dim i As Integer
    Dim ws As Worksheet
 
    Application.EnableEvents = False
    Application.ScreenUpdating = False
 
    For Each ws In ActiveWorkbook.Sheets
        For i = 2 To 31
            If ws.Cells(1, i).Value = "" Then
                ws.Columns(i).Hidden = True
            Else
                ws.Columns(i).Hidden = False
            End If
        Next
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
I would make the 2 to 31 a range, probably a sheet named range incase it changes but this should get you started
Yep, missed that you need to wrap the code in this: If ws.Index >= 5 And ws.Index <= 100 Then....
 
Upvote 0
This should work
VBA Code:
Private Sub Workbook_Open()
    Dim i As Integer
    Dim ws As Worksheet
 
    Application.EnableEvents = False
    Application.ScreenUpdating = False
 
    For Each ws In ActiveWorkbook.Sheets
        For i = 2 To 31
            If ws.Cells(1, i).Value = "" Then
                ws.Columns(i).Hidden = True
            Else
                ws.Columns(i).Hidden = False
            End If
        Next
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
I would make the 2 to 31 a range, probably a sheet named range incase it changes but this should get you started
Thank you, this works perfectly!!

As I understand it, this goes through every sheet in the workbook. I would like it to only run in sheets 5-100 of the workbook i.e. miss out the first 4 sheets which have information that is never going to change.

I understand I could just populate cells(1,i) with something in the first four sheets of the workbook and hide the first row, but I would prefer the code to only miss out these first four sheets.

Once again, thank you. So much to learn with VBA 😁
 
Upvote 0
This should do it

VBA Code:
Public Sub HideColumns()

    Dim i As Integer
    Dim ws As Worksheet
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    For Each ws In ActiveWorkbook.Sheets
        If ws.Index >= 5 And ws.Index <= 100 Then
            For i = 2 To 31
                ws.Columns(i).Hidden = ws.Cells(1, i).Value = ""
            Next
        End If
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This should do it

VBA Code:
Public Sub HideColumns()

    Dim i As Integer
    Dim ws As Worksheet
   
    Application.EnableEvents = False
    Application.ScreenUpdating = False
   
    For Each ws In ActiveWorkbook.Sheets
        If ws.Index >= 5 And ws.Index <= 100 Then
            For i = 2 To 31
                ws.Columns(i).Hidden = ws.Cells(1, i).Value = ""
            Next
        End If
    Next

    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
This has worked perfectly, thank you!!
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,966
Members
449,094
Latest member
Anshu121

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