For Each Next Loop

I have a very simple For Each Next loop, where I want to loop through all the worksheets in the active workbook and simply select cell A1. It just isn't having it, giving me the error 'Select Method of Range Class failed'.

Here is the code:

Dim Wksht As Worksheet

For Each Wksht In ActiveWorkbook.Worksheets
Wksht.Range("A1").Select
Next Wksht

Cheers for any help!



Answer this question

For Each Next Loop

  • jods

    Just to nitpick:

    I'd suggest replacing "Wksht.Select" with "Wksht.Activate". Theoretically, the worksheets referred to could be trapping SelectionChange events, and it might not be appropriate to pass the entire worksheet range to that event handler. (No, this probably isn't the case, but why take chances )



  • Syed Atheeque Pasha

    KeithyBOY,

    In order to select a cell the worksheet needs to be active, you can't select a cell in a inactive worksheet, here's the code you need

    Dim Wksht As Worksheet

    For Each Wksht In ActiveWorkbook.Worksheets
    Wksht.Select
    Wksht.Range("A1").Select
    Next Wksht



  • For Each Next Loop