VBA code to restart S/N from 1

CalKlein

New Member
Joined
Jan 25, 2015
Messages
3
Hi, I'm new to VBA and would hope to get some help here.
At column D, I have sorted all "B" follow by "P".
I have the undermentioned code to list the S/N for column A from 1 onwards.
But I would need the S/N to start from one again once the Column D value is "P".
Your assistance is very much appreciated.

Seems like I can't upload a picture so appreciate your help with above wordings.
Thanx.

Dim k As Integer
Dim LR As Integer
'Find the last row with data
LR = Range("D" & Rows.Count).End(xlUp).Row

For k = LR To 2 Step -1
If Cells(k, 4).Value = "B" Then
Cells(k, 1).Value = k - 1
ElseIf Cells(k, 4).Value = "P" Then
Cells(k, 1).Value = k - 2
End If
Next k
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to MrExcel.

Try:

Code:
Sub Test()
    Dim LR As Integer
'   Find the last row with data
    LR = Range("D" & Rows.Count).End(xlUp).Row
    With Range("A2:A" & LR):
        .FormulaR1C1 = "=COUNTIF(R2C4:RC4,RC4)"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Hi Andrew,

What would the formula code look like to do the equivalent of this?

=D2&" - "&COUNTIF($D$2:$D2,$D2)
.Value = .Value

B - 1
B - 2
p - 1
p - 2

Howard
 
Upvote 0
Perhaps I should have given this a little more thought before posting...

Code:
Sub Test()
    Dim LR As Integer
'   Find the last row with data
    LR = Range("D" & Rows.Count).End(xlUp).Row
    With Range("E2:E" & LR):
        '.FormulaR1C1 = "=COUNTIF(R2C4:RC4,RC4)"
        .Formula = "=D2&"" - ""&COUNTIF($D$2:$D2,$D2)"
        .Value = .Value
    End With
End Sub

Howard
 
Upvote 0
Welcome to MrExcel.

Try:

Code:
Sub Test()
    Dim LR As Integer
'   Find the last row with data
    LR = Range("D" & Rows.Count).End(xlUp).Row
    With Range("A2:A" & LR):
        .FormulaR1C1 = "=COUNTIF(R2C4:RC4,RC4)"
        .Value = .Value
    End With
End Sub

Thank you very much, Sir! The code works perfectly.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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