Key - start with 1 and add 1 each time

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello all


I am trying to add a Primary key in column "A" starting at "A13"

If there is a value in column "C" then add a number in column "A"

IE: "A13" would be 1
"A14" would be 2
"A15" would be 3

Below is a start but it needs to look at column "C"

and so on

Code:
Sub Key()
    Dim rng As Range
    Set rng = Range("A13")
    If IsEmpty(rng) Then
        rng = 0
    Else
        If Not IsEmpty(rng.Offset(1, 0)) Then
            Set rng = rng.End(xlDown)
        End If
        rng.Offset(1, 0) = rng.Value + 1
    End If
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
How about
Code:
Sub VbaHell()
   Dim cl As Range
   Dim i As Long
   
   For Each cl In Range("A13", Range("C" & Rows.Count).End(xlUp).Offset(, -2))
      If cl.Offset(, 2) <> "" Then
         i = i + 1
         cl.Value = i
      End If
   Next cl
End Sub
 
Upvote 0
You could do this with a simple formula:
In A13:
=IF(A12="value of whatever is in A12<value of="" a12="">",1,A12 + 1)</value>
 
Upvote 0
Thanks for your reply GR0007, this would work but I will go with Fluff as I need to incorporate the code in an existing code module
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,473
Messages
6,130,838
Members
449,597
Latest member
buikhanhsang

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