I have exactly the same columns to hide across several workbooks.
Unfortunately, the following code doesn't work well:
Dim sh As Worksheet
Dim sNames As Variant
sNames = Array("BoM North", "BoM North Extras", "BoM South")
Dim BomColumns As Range
Set BomColumns = Columns("I:J")
For Each sh In sNames
Sheets(sh).BomColumns.Hidden = True
Next was
As I a getting an error: Object required on the following line:
For each sh in Shnames
The code was picked up from here:
https://www.mrexcel.com/board/threads/vba-to-hide-columns-on-multiple-sheets.961675/
What object should be assigned here?
UPDATE:
With the following code:
Dim ws As Worksheet 'Dim wsq As Worksheet
Dim BomColumns As Range Set BomColumns = Columns("I:J")
For Each ws In Sheets(Array("BoM North", "BoM North Extras", "BoM South"))
With ws.Range(BomColumns)
.Hidden = True
End With
Next was
I have an error: Method 'Range of Worksheets' failed at the following line: With ws.Range(BomColumns)
Sheets(sh).BomColumns.Hidden = TruewithSheets(sh).range(BomColumns.address).EntireColumn.Hidden = True.Set BomColumns = Columns("I:J")sets a range in the active sheet. So you cannot use it in other sheets. But its address, you can... You can do it without preliminarily setting a range:Sheets(sh).Columns("I:J").Hidden = True...Sheets(sh).Columns("I:J").Hidden = True.shmust be declared asVariantor asString. It is not a worksheet... Your code iterates between strings of the array. I did not observe initially where the error is raised.