Want to apply vba code to selected sheet in one go.

Harshil Mehta

Board Regular
Joined
May 14, 2020
Messages
85
Office Version
  1. 2013
Platform
  1. Windows
The code dynamically hides rows & columns. I want to apply this code in one go to multiple sheets.
Sheets Names are based on cell value which means if the value in the cell changes, sheet name also changes.
I am new to VBA coding, would be thankful if anyone could help me out with this.

VBA Code:
Sub Hide_Rows_Columns()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets(Array(Sheets(2), Sheets(3), Sheets(4)))

With Range("B:B")
Range(.Cells(Rows.Count, 2), .Cells(Rows.Count, 2).End(xlUp).Offset(2, 0)).EntireRow.Hidden = True
End With

With Range("10:10")
Range(.Cells(1, .Columns.Count), .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 2)).EntireColumn.Hidden = True
End With

Next

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello,
Where would the sheet names be?
Capture.PNG
 
Upvote 0
This should get it done.

VBA Code:
Sub Button1_Click()
    Dim ws As Worksheet, sh As Worksheet
    Dim rng As Range, c As Range

    Set ws = Sheets("User_Dashboard")
    With ws
        Set rng = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For Each c In rng.Cells
            For Each sh In Sheets
                If sh.CodeName = c Then
                    With sh
                        .Name = c.Offset(, 1)
                        With .Range("B:B")
                            Range(.Cells(.Rows.Count, 2), .Cells(.Rows.Count, 2).End(xlUp).Offset(2, 0)).EntireRow.Hidden = True
                        End With
                        With .Range("10:10")
                            Range(.Cells(1, .Columns.Count), .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 2)).EntireColumn.Hidden = True
                        End With
                    End With
                End If
            Next sh
        Next c
    End With



End Sub
 
Upvote 0

Forum statistics

Threads
1,214,840
Messages
6,121,895
Members
449,058
Latest member
Guy Boot

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