简体   繁体   中英

Error 1004 - 'select method of worksheet class failed' when worksheet is Selected

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).

Name , Index and CodeName are three properties of a worksheet, which are quite different. In the code from the screenshot, Sheet21 is a CodeName and it probably corresponds to Worksheets(17) .

To see the difference, run the code below and take a look at the immediate window:

Sub TestMe()
    Dim i As Long
    For i = 1 To Worksheets.Count
        Debug.Print Worksheets(i).Name
        Debug.Print Worksheets(i).Index
        Debug.Print Worksheets(i).CodeName
        Debug.Print "-----------------------"
    Next i
End Sub

True and false would not help in If Worksheets(21).Visible = False , if the sheet is xlVeryHidden . xlVeryHidden is evaluated to 2 and this is evaluated to True .

Long story short:

Write Worksheets(21).Visible = xlVisible on the line before the select. Or Sheet21.Visible = xlVisible , as Worksheets(21) does not exist in your data.

And in general:

You're attempting to select a sheet by it's index, and you're overreaching because you've deleted some of your sheets.

The tab called Sheet21 is likely referred to by index as Sheets(16) or Workheets(16) . I can't be sure as I can't see the order of your tabs and the 16 here refers to the 16th tab as displayed on the tab bar (including hidden etc.) in order .

The following should work, regardless of the visibility of the tab when run.

Sub AddColumns()
  'Inserts Four Columns at L:O - Q3 Week High tab

  Sheet21.Range("L:O").EntireColumn.Insert
  'Format colour

  With Sheet21.Range("L4:N55").Interior
      .Pattern = xlNone
      .TintAndShade = 0
      .PatternTintAndShade = 0
  End With

If you want to force it to be visible, just use:

Sheet21.Visible = xlVisible

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM