ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Stepping through v. running code (https://www.excelbanter.com/excel-programming/416777-stepping-through-v-running-code.html)

Office_Novice

Stepping through v. running code
 
Greetings,
When i step through this it works perfectly. However when i run it it only
returns the first "i" for each any thoughts?

Sub PrintEmOut()
Dim MyRange As Range
Dim WS As Worksheet
Dim Lastrow As Long
Dim i As Variant

Set WS = ActiveWorkbook.Worksheets(2)
Lastrow = WS.Cells(Cells.Rows.Count, "B").End(xlUp).Row
Set MyRange = WS.Range("B2:B" & Lastrow)

For Each i In MyRange
With Worksheets(1)
.Label1.Caption = i
.Label2.Caption = i.Offset(0, 13).Value
.Label3.Caption = i.Offset(0, 11).Value
.PrintPreview
.Label1.Caption = ""
.Label2.Caption = ""
.Label3.Caption = ""
End With
Next
End Sub


joel

Stepping through v. running code
 
If you have more than one workbook open make sure you have the one you are
modifying active before steopping through code. I also added activeworkbook
to the with statement.

from
For Each i In MyRange
With ActiveWorkbook.Worksheets(1)
..
..
..
End With
Next

to
For Each i In MyRange
With Worksheets(1)
..
..
..
End With
Next

"Office_Novice" wrote:

Greetings,
When i step through this it works perfectly. However when i run it it only
returns the first "i" for each any thoughts?

Sub PrintEmOut()
Dim MyRange As Range
Dim WS As Worksheet
Dim Lastrow As Long
Dim i As Variant

Set WS = ActiveWorkbook.Worksheets(2)
Lastrow = WS.Cells(Cells.Rows.Count, "B").End(xlUp).Row
Set MyRange = WS.Range("B2:B" & Lastrow)

For Each i In MyRange
With Worksheets(1)
.Label1.Caption = i
.Label2.Caption = i.Offset(0, 13).Value
.Label3.Caption = i.Offset(0, 11).Value
.PrintPreview
.Label1.Caption = ""
.Label2.Caption = ""
.Label3.Caption = ""
End With
Next
End Sub



All times are GMT +1. The time now is 04:06 AM.

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