+ Reply to Thread
Results 1 to 3 of 3

Autonumber

Hybrid View

NaomiDawn Autonumber 11-29-2013, 05:35 AM
XOR LX Re: Autonumber 11-29-2013, 05:59 AM
protonLeah Re: Autonumber 11-29-2013, 05:48 PM
  1. #1
    Registered User
    Join Date
    11-25-2013
    Location
    England
    MS-Off Ver
    Excel 2007, Access 2007
    Posts
    14

    Autonumber

    Hi,

    I'm looking for a piece of code that can add an auto number to a cell when the rest of the row is populated.

    It doesn't need to have a pre-fix. I've tried this:

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim myVal As Variant
        If Target.Column <> 2 Then Exit Sub
    
        If Target.Row = 4 Then
            Target.Offset(0, -1).Value = "0001"
        Else
            myVal = Split(Target.Offset(-1, -1).Value, "-")(1)
            Target.Offset(0, -1).Value = "001-" & Format(Val(myVal + 1), "0000")
        End If
        
    End Sub
    And it works! But only for one row - it wont take the code down onto the next row.

    Can anyone give me any advice?

    Thanks

    Naomi
    Last edited by NaomiDawn; 12-05-2013 at 05:15 AM.

  2. #2
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: Autonumber

    Hi,

    I'm no expert in VBA, but it appears that simply:


    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim myVal As Variant
    
    If Target.Column <> 2 Then Exit Sub
    
    If Target.Row = 4 Then
    
    Target.Offset(0, -1).Value = "0001"
    
    Else
    
    myVal = Target.Offset(-1, -1) + 1
    Target.Offset(0, -1).Value = Format(myVal, "0000")
    
    End If
    
    End Sub
    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,963

    Re: Autonumber

    A couple of thoughts.
    I'm not sure that you need a macro, since you want the number in column B whenever there is data "somewhere" in rows 4 and beyond. For instance, you could use a cell formula that checks for data in column A and uses the row number to calculate the serial number in column B:
    B4: =IF(A4<>"",ROW()-3,"") and copied down will put 1,2,... whenever you enter data in column A.
    If you want a prefix you could format column B using "001-"0000
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Autonumber Until Certain Value then Restart
    By sperryxx in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-05-2013, 04:45 PM
  2. [SOLVED] Trying to autonumber
    By nonstopcrunchy in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-29-2013, 02:51 PM
  3. [SOLVED] Autonumber
    By nik7 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-23-2013, 06:50 PM
  4. Autonumber
    By jonnygrim in forum Excel General
    Replies: 2
    Last Post: 07-17-2008, 05:22 PM
  5. [SOLVED] autonumber
    By Hardy in forum Excel General
    Replies: 4
    Last Post: 11-24-2005, 01:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1