ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   which sheet a loop is valid for-how do you know? (https://www.excelbanter.com/excel-programming/370116-sheet-loop-valid-how-do-you-know.html)

sverre

which sheet a loop is valid for-how do you know?
 
How do I know which sheet the loop refers to / is valid for? According to my
logic it should be previous sheet, but it seems it is the other sheet (start
here)? Not logical as I select the previous sheet just before the loop starts.

Sheets("start here!").Activate
ActiveSheet.Previous.Select

x = 2

Do While Cells(x, 2).Value < ""

Cells(x, 9) = Application.VLookup(Cells(x, 8),
Worksheets("Prognos").Range("A1:B50000"), 2, False)

x = x + 1

Loop

End Sub

Tom Ogilvy

which sheet a loop is valid for-how do you know?
 
In a general module, you would be correct.

I suspect this is in the worksheet module of the sheet "Start Here!" where
unqualified references are to the sheet containing the code, not the
activesheet.

Qualify your references or move the code to a general module and call it
from the worksheet module (if necessary)

--
Regards,
Tom Ogilvy


"sverre" wrote:

How do I know which sheet the loop refers to / is valid for? According to my
logic it should be previous sheet, but it seems it is the other sheet (start
here)? Not logical as I select the previous sheet just before the loop starts.

Sheets("start here!").Activate
ActiveSheet.Previous.Select

x = 2

Do While Cells(x, 2).Value < ""

Cells(x, 9) = Application.VLookup(Cells(x, 8),
Worksheets("Prognos").Range("A1:B50000"), 2, False)

x = x + 1

Loop

End Sub



All times are GMT +1. The time now is 11:38 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com