Help needed on macro for project!

Shaun_Lin

New Member
Joined
Jun 22, 2016
Messages
4
Hi,

I am trying to write a macro to assign a set new of serial number to each row. For example, each row will be given a number (in sequence) after every 8 rows and when there are no other numbers below it, the code will go back up again to continue the pattern again. Any help on this will be very much appreciated. Thank you!:laugh::laugh::laugh:


[TABLE="width: 287"]
<colgroup><col style="margin-left: 40px;"><col style="margin-left: 40px;"></colgroup><tbody>[TR]
[TD]
OLD
NUMBER​
[/TD]
[TD]
NEW
SERIAL NUMBER​
[/TD]
[/TR]
[TR]
[TD]
1​
[/TD]
[TD]
1​
[/TD]
[/TR]
[TR]
[TD]
2​
[/TD]
[TD]
4​
[/TD]
[/TR]
[TR]
[TD]
3​
[/TD]
[TD]
7​
[/TD]
[/TR]
[TR]
[TD]
4​
[/TD]
[TD]
10​
[/TD]
[/TR]
[TR]
[TD]
5​
[/TD]
[TD]
13​
[/TD]
[/TR]
[TR]
[TD]
6​
[/TD]
[TD]
16​
[/TD]
[/TR]
[TR]
[TD]
7​
[/TD]
[TD]
19​
[/TD]
[/TR]
[TR]
[TD]
8​
[/TD]
[TD]
22​
[/TD]
[/TR]
[TR]
[TD]
9​
[/TD]
[TD]
2​
[/TD]
[/TR]
[TR]
[TD]
10​
[/TD]
[TD]
5​
[/TD]
[/TR]
[TR]
[TD]
11​
[/TD]
[TD]
8​
[/TD]
[/TR]
[TR]
[TD]
12​
[/TD]
[TD]
11​
[/TD]
[/TR]
[TR]
[TD]
13​
[/TD]
[TD]
14​
[/TD]
[/TR]
[TR]
[TD]
14​
[/TD]
[TD]
17​
[/TD]
[/TR]
[TR]
[TD]
15​
[/TD]
[TD]
20​
[/TD]
[/TR]
[TR]
[TD]
16​
[/TD]
[TD]
23​
[/TD]
[/TR]
[TR]
[TD]
17​
[/TD]
[TD]
3​
[/TD]
[/TR]
[TR]
[TD]
18​
[/TD]
[TD]
6​
[/TD]
[/TR]
[TR]
[TD]
19​
[/TD]
[TD]
9​
[/TD]
[/TR]
[TR]
[TD]
20​
[/TD]
[TD]
12​
[/TD]
[/TR]
[TR]
[TD]
21​
[/TD]
[TD]
15​
[/TD]
[/TR]
[TR]
[TD]
22​
[/TD]
[TD]
18​
[/TD]
[/TR]
[TR]
[TD]
23​
[/TD]
[TD]
21​
[/TD]
[/TR]
[TR]
[TD]
24​
[/TD]
[TD]
24​
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG22Jun29
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] c, st [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer,[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("a" & Rows.Count).End(xlUp))
num = Rng.Count / 8
c = 1: st = 1
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   Dn.Offset(, 1) = c: n = n + 1
   [COLOR="Navy"]If[/COLOR] n Mod 8 = 0 [COLOR="Navy"]Then[/COLOR]
      st = st + 1: c = st
   [COLOR="Navy"]Else[/COLOR]
       c = c + num
   [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Sub macro4()

Dim lastrow1 As Long
Dim k As Long
Dim l As Long
Dim j As Long

k = 2
l = 1

With ActiveSheet
lastrow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For i = 1 To lastrow1 - 1

j = k + (l - 1) * 8
If j > lastrow1 Then
k = k + 1
l = 1
j = k + (l - 1) * 8
End If

Cells(j, 2).Value = i
l = l + 1
Next
Cells(1, 2).Value = "New Series"
End Sub
 
Upvote 0
Dear MickG and bhos123,

Thanks for the prompt reply and help! The solutions work fine and I was able to solve the problem with that. :)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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