Hi I have a problem with my code that is receiving the following error right at the last part of it:
Error 1004 - 'select method of worksheet class failed'
The code is basically a repetition of pretty much the same code that is doing stuff in 3 different tabs, the problem occurs at the very last tab (sheet21) and I can't understand why as it's working for 2 other tabs with no problem.
So the code for each tab looks the same with differences in excel formulas that are being populated in the table only.The problem occurs in sheet selection, sheet9, sheet11 and sheet21, the last one is where I receive an error, the previous tabs are being executed without any problem within the proper Tabs(sheets).
The whole code is very long and is adding columns with formatting and formulas, so here is just the part where I'm receiving an error:
Sub AddColumns()
'Inserts Four Columns at L:O - Q3 Week High tab
Worksheets(21).Range("L:O").EntireColumn.Insert
'Format colour
Worksheets(21).Select '-------1004 ERROR HERE
Range("L4:N55").Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
the worst part is that exactly the same construction of the code works for Worksheets(9) and Worksheets(11) that are being executed before in one go but as soon as I reach Worksheet(21) I'm receiving 1004 error and the process stops.
I've tried:
If Worksheets(21).Visible = False Then Sheets(21).Visible = True
Sheets(21).Select
but then the code was being executed in some completely different tab Worksheet(19) for some reason and I don't understand why.
I'm using "Worksheet(#)" naming convention as the tabs are changing names on regular basis and using this format seemed to work fine before I've added Worksheet(21).