Macro to automatically number rows based on a number

sean_nyc

New Member
Joined
Nov 26, 2014
Messages
3
Hi there,

I am new to the group and was curious if anyone can help me out with a macro I need to create. I am trying to number rows based on a number in the header. However, I want to repeat a number a certain number of times until it moves on to the next number.

To provide more detail:
Number of rows I want to have the same number for: 2
A1: 5

Expected result:
Row 2: 1
Row 3: 1
Row 4: 2
Row 5: 2
Row 6: 3
Row 7: 3
Row 8: 4
Row 9: 4
Row 10: 5
Row 11: 5

This will then need to be extrapolated to be repeated for 45 rows.

Thank you!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
sean_nyc,

Welcome to MrExcel.

Not sure if I'm understanding the 'repeated for 45 rows' ????

Try something like....

Code:
Sub NumberRows()
m = 45 'last row *******??????
c = 2 'same number
n = 0  
For r = 2 To m Step c
n = n + 1
Cells(r, 1) = n
Cells(r, 1).Offset(1, 0) = n
If n = Cells(1, 1) Then n = 0
Next r
End Sub

Hope that helps.
 
Last edited:
Upvote 0
Welcome to Mr Excel

You can achieve this using a formula.

Something like this


A
B
C
1
5​
Repeat​
2
1​
2​
3
1​
4
2​
5
2​
6
3​
7
3​
8
4​
9
4​
10
5​
11
5​
12

Formula in A2 copied down for how many rows you need

=IF($A$1*$C$2>=ROWS(A$2:A2),INT((ROWS(A$2:A2)+$C$2-1)/$C$2),"")

Hope this helps

M.
 
Upvote 0
@ Marcelo Branco: Thank you so much! This is exactly what I needed! Will definitely archive this formula for future use!
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,947
Members
449,480
Latest member
yesitisasport

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