Variable in macro loop

paget

New Member
Joined
Mar 15, 2019
Messages
6
I need to run a FOR NEXT loop in a macro for a specific number of times depending upon the maximum number in a column of a worksheet. Can this be achieved to avoid me having to manually edit the VBA every time I want to run the macro?:confused:
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi, welcome to the forum!

You could try something like this.

Code:
Sub Example()
Dim i As Long
For i = 1 To Application.Max(Sheets("Sheet1").Columns("A"))
    'do stuff
Next i
End Sub
 
Upvote 0
Thank you for this and please excuse my lack of understanding.
If I use the dimension statement and then want to perform a calculation to get the variable i, can I not just refer to the range where the data to be tested resides? For instance, the loop needs to run a number of times defined by the integer value of 1000000 divided by the maximum number in the range 'seacalc'.
I tried:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]For i = 1 To Int(1000000 / Application.Max(seacalc))
but that did not work.
[/FONT]
 
Upvote 0
OK - try like this:

Code:
For i = 1 To Int(1000000 / Application.Max(Range("seacalc")))
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
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