View Single Post
  #13   Report Post  
Posted to microsoft.public.excel.programming
Jacob Skaria Jacob Skaria is offline
external usenet poster
 
Posts: 8,520
Default For Each Next Loop behaving in unexpected manner

Check out the whether the below makes it any faster...


Dim ws As Worksheet, wsActive As Worksheet, lngRow As Long
Set wsActive = ActiveSheet
Application.ScreenUpdating = False
For intSheet = 1 To Sheets.Count - 1
lngRow = Sheets(intSheet).Cells(Rows.Count, "W").End(xlUp).Row
Application.Goto Sheets(intSheet).Range("W" & lngRow + 9)
Next
wsActive.Activate
Application.ScreenUpdating = True


--
Jacob


"MichaelDavid" wrote:

Hi Jacob:
I just noticed a nice advantage of your method: By using the "For intSheet
= 1 To Sheets.Count - 1" instruction, you eliminate the necessity of my If
Then Else Loop. Nice going!
--
May you have a most blessed day!

Sincerely,

Michael Fitzpatrick


"Jacob Skaria" wrote:

Try the below..

Sub Macro()
Dim ws As Worksheet, lngRow As Long
For intSheet = 1 To Sheets.Count - 1
lngRow = Sheets(intSheet).Cells(Rows.Count, "W").End(xlUp).Row
Application.Goto Sheets(intSheet).Range("G" & lngRow + 9)
Next
Sheets(1).Activate
End Sub


--
Jacob


"MichaelDavid" wrote:

Hi Tom:
I played around with the code for a few hours, and here is what I came up
with:

Dim WS As Worksheet
For Each WS In ActiveWorkbook.Worksheets
If WS.Name < "Sheet1" Then
LstRowData = WS.Range("O2")
WS.Activate
WS.Cells(LstRowData + 9, "W").Select
End If
Next WS

which is very similar to what you came up with.

Apparently the WS.Activate instruction is absolutely necessary. Excel does
not allow
a cell to be selected unless the worksheet it is on has been activated.
Perhaps you can come up with a way to omit WS.Activate instruction. Just
removing the WS.Activate instruction results in the error message: "Run-time
error '1004': Select method of Range class failed." Thanks for all your kind
help.

--
May you have a most blessed day!

Sincerely,

Michael Fitzpatrick


"tompl" wrote:

I think I get it now. First you have to select the worksheet, then you can
select the cell.

Try this:

For Each WS In ActiveWorkbook.Worksheets
If WS.Name < "Sheet1" Then
LstRowData = Range("O2") 'if LstRowData was computed elsewhere why is
'this here? should it be WS.Range("O2") = LstRowData? I think this line
'might need to be eliminated here. Or identify what sheet Range("O2") is on.
WS.select
Cells(LstRowData + 9, "W").Select
End If
Next