Looping X number

hatstand

Well-known Member
Joined
Mar 17, 2005
Messages
778
Office Version
  1. 2016
Platform
  1. Windows
Hi, I am trying to get the code below to loop down X number of times. With maxi being a dynamic range name. It copies down cell A2 and adds 1 each time. But to the length maxi declares. If i use a number it works but not if I declare it.

Can someone tell me what I'm doing wrong please.

Sub for_demo()

Dim Add1 As Integer
Dim maxi As Variant

For Add1 = 1 To maxi Step 1

ActiveCell.Value = Range("A2").Value + 1
ActiveCell.Offset(1, 0).Select

Next

End Sub
 
Sorry hatstand, I thought "maxi" was a named range.

Luckily there's plenty of other clever solutions offered :)
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Thanks. Trebor. I'm still stuck with the problem. The help has been great and much appreciated. but I still can't understand why I can't use a dynamic range to decide how many times to loop.

I guess I'll have to try another approach. Thanks for everyones help.
 
Upvote 0
Hatstand,

Did you try code in post#10?

Did it do as you have requested?

If not, what problem did you have with it? e.g. wrong result, error message, failed to do anything at all, other ???

Providing useful feedback is likely to be helpful to everyone, including yourself.
 
Upvote 0
What I figured out is that dynamic range values would or can't be picked up as the variable. So I used:
For Add1 = 1 To Range("Count").Value Step 1
Then simply put an =counta() in the sheet with the range to be counted.

I would imagine all of the replies would have worked, but my VBA knowledge is brilliant so couldn't get them to work.

I really appreciate the time and help offered. Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,259
Members
452,901
Latest member
LisaGo

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