numbering macro not working....

cbrf23

Board Regular
Joined
Jun 20, 2011
Messages
241
Hi, I wrote a macro that is suppposed to add a number to column a when I enter data in column b. I dont want this to be the row number, i just want sequential numbering of these items. I'm not getting any errors....but I'm also not getting any numbers. Can anyone tell me what I'm doing wrong?

Code:
Private Sub AUTO_NUMBER(ByVal Target As Range)
Dim ItemNum
On Error GoTo ERRHANDLER
ItemNum = Value.Target.Offset(-1, -1) + 1

 
    If (Not Intersect(Target, Range("B:B")) Is Nothing) Then
            If (Not IsEmpty(Target)) Then
                    Target.Offset(0, -1) = ItemNum
                Else
                    With Target.Offset(0, -1)
                    .ClearContents
                    End With
            End If
    End If
 
Exit Sub
ERRHANDLER:
MsgBox "SOMETHINGS NOT RIGHT..."
Exit Sub
 
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If you want it to run automatically it would need to be

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
Upvote 0
Try this instead [it seems to work]:

Code:
Private Sub AUTO_NUMBER(ByVal Target As Range)
Dim ItemNum
On Error GoTo ERRHANDLER
    If (Not Intersect(Target, Range("B:B")) Is Nothing) Then
            If (Not IsEmpty(Target)) Then
                    [COLOR=red][B]ItemNum = Target.Offset(-1, -1).Value + 1[/B][/COLOR]
                    Target.Offset(0, -1) = ItemNum
                Else
                    With Target.Offset(0, -1)
                    .ClearContents
                    End With
            End If
    End If
Exit Sub
ERRHANDLER:
MsgBox "SOMETHINGS NOT RIGHT..."
Exit Sub
 
End Sub

Your code was erroring on:

ItemNum = Value.Target.Offset(-1, -1) + 1
 
Upvote 0
thank you VoG.

doofusboy, thank you very much.

Everything is working good now!
 
Last edited:
Upvote 0
So, I've been playing a little with my error handler and I'm trying to get it to automatically update my numbers on a row/multiple row delete?

I'd like it if I delete a row, that it would re-number from that point down so the numbers dont get out of order...

So...if I have entries from B3 to B23 and I delete row 10, then rows 11-23 should get re-numbered in sequence.

Does that make sense?

I tried adding into the error handler something like:
Code:
If Err.Number = 1004 Then  '<---1004 is error code returned when I deleted rows.
           If (Not IsEmpty(Target)) Then
                    ItemNum = Target.Offset(-1, -1).Value + 1
                    Target.Offset(0, -1) = ItemNum
                Else
                    With Target.Offset(0, -1)
                    .ClearContents
                    End With
            End If
     Do Until Target = ""
          Target.offset(-1,0).Select
          Loop
Exit Sub

But that did not seem to work....
 
Upvote 0

Forum statistics

Threads
1,224,558
Messages
6,179,512
Members
452,920
Latest member
jaspers

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