Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default For Each Next Loop behaving in unexpected manner

Hi Jacob:
Thanks for all your help and suggestions. Yes, the code runs very
quickly. Here is what I finally settled on--works perfectly:

Dim intSheet As Long
Application.ScreenUpdating = False
For intSheet = 1 To Sheets.Count - 1
Application.Goto Sheets(intSheet).Range("W" & Sheets(intSheet).Range("O2") +
9)
Next
Application.ScreenUpdating = True
Sheets(1).Activate

It selects the desired cell on each sheet. (On some sheets there is a lot of
data; on others just a little.) And it ends up with Sheets(1) activated so I
can quickly flip through the sheets in order comparing data.
--
May you have a most blessed day!

Sincerely,

Michael Fitzpatrick


"Jacob Skaria" wrote:

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data from SQL to Excel in a formated manner.. chintu4uin Excel Programming 0 February 16th 08 08:52 AM
comapre two list of data & arranging in a same manner Sachin New Users to Excel 1 November 25th 06 01:28 PM
How do I add up row sums in a systematic manner? wicked_rich Excel Programming 1 August 15th 05 03:54 PM
shell to behave in synchronous manner rraajjiibb Excel Programming 1 May 28th 04 08:52 PM
macro needed for sorting txt in a ceratian manner Martyn Excel Programming 1 January 24th 04 02:13 PM


All times are GMT +1. The time now is 02:27 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"