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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Where abouts is maxi getting a value from?

If it's a named range in a sheet then

Code:
For Add1 = 1 To range("maxi").value Step 1
and lose the 'dim' reference
 
Upvote 0
what is your code doing
Try this
Sub for_demo()

Dim Add1 As Integer
Dim maxi As Integer
maxi = InputBox("Insert Number")
For Add1 = 1 To maxi Step 1

Range("A" & Add1).Value = ActiveCell.Value

Next

End Sub
 
Upvote 0
'maxi' is a dynamic range formula: =OFFSET(Sheet1!$C$1,0,0,COUNT(Sheet2!$C:$C),1)

It gives me a count of entries into column C.
 
Upvote 0
My code copies cell 'A2' value, then adds 1 then moves down 1 cell. My rpblem is that I want' maxi' my dynamic range formula to decide how many times to copy down.
 
Upvote 0
If you're using code anyway, why not count the number of entries in column C in VB?

or maybe, since maxi is defining a range, you could replace the whole thing with this?

Code:
for each c in range("maxi").cells
c.value=range("A2")+1
next c
 
Upvote 0
Try this code , may can solve your Problem

Code:
Sub Test()
Dim LR As Long
LR = Range("C" & Rows.Count).End(xlUp).Row
For i = 3 To LR
    Cells(i, 1).Value = Cells(i - 1, 1) + 1
Next i
End Sub
 
Upvote 0
Here's a slightly different way to do it in one swoop:

Code:
With Range("maxi")
        .Formula = "=ROW()-1"
        .Value = .Value
    End With

Regards,

Robert
 
Upvote 0
Hi,

Sorry to both of you. I can't get either to work. My fault I know, as I'm still learning the VBA thing.
 
Upvote 0
Maybe this one?
Code:
Sub hatstand()
Range("A1") = 17 'or whatever
maxi = 6    'or whatever
Range("A1").Resize(maxi).DataSeries
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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