VBA dynamic value from cell for row count

jblonde002

Board Regular
Joined
Jun 10, 2014
Messages
61
I have some VBA in a workbook that I didn't create but need to edit. The issue faced is that at the moment it is referencing a count of the number of rows in column T to determine how many times to loop the printing (which is always 15 rows). However I want to use the value in cell U2 to determine how many times to loop through for example if U2 has the number 5 for the code to loop five times.

Can anyone explain how I might achieve that? Many thanks...

Code:
    If MsgBox("Are you sure you want to print ALL the Progress Trackers for the teacher you have selected?", vbYesNo) = vbYes Then    Dim i As Long
       For i = 1 To Sheets("Codes").Cells(Rows.Count, "T").End(xlUp).Row
            With Sheets("My Classes")
                .Range("K3").Value = Sheets("Codes").Cells(i, "T").Value
                Application.Calculate
                .PrintOut ' 
            End With
        Next i
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Just change this line:
Code:
For i = 1 To Sheets("Codes").Cells(Rows.Count, "T").End(xlUp).Row
to this:
Code:
For i = 1 To [COLOR=#ff0000]Range("U2").Value[/COLOR]
Note that if you are not on the same sheet as this U2 value when running, you will need to add a Sheet reference in front of it, i.e.
Code:
For i = 1 To [COLOR=#ff0000]Sheets("Codes").R[/COLOR][COLOR=#FF0000]ange("U2").Value[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top