+ Reply to Thread
Results 1 to 11 of 11

How to implement Customer ID auto increment when using alpha numeric values with VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    02-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    22

    How to implement Customer ID auto increment when using alpha numeric values with VBA

    I have created a simple UserForm to enter new customer details to the Customer List in the spreadsheet, form works fine except for one little thing, which is New Customer ID, which works, but only with numeric values like 1, 2, 3 and so on.

    Basically what I need this for to do is once form is opened/called new customer ID need to be created, which could be and Alfa numerical set of characters like AA-01234, AA-01235, AA-01236 and so on.

    Also, is there a way of posting newly added Customer ID in the MsgBox along with MsgBox "One record added to Customers List. New Customer ID is "

    All of my attempts to create this are failing and causing errors, which I really cannot figure out since I am new to VBA and had never used it until now.

    Please help me a little.

    Here is my code, Customer ID is TextBox1.

    Thanks in advance
    Private Sub UserForm_Activate()
    Dim iRow As Long
    Dim ws As Worksheet
    
        Set ws = Worksheets("Customers")
    
        RefNo.Enabled = True
        'find last data row from database
        iRow = ws.Cells(Rows.Count, 8).End(xlUp).Row
    
        If ws.Range("A" & iRow).Value = "" Then
            RefNo.Text = "TAS1"
            ws.Range("A" & iRow).Value = RefNo
        Else
            RefNo.Text = "TAS" & Val(Mid(ws.Cells(iRow, 1).Value, 4)) + 1
            ws.Range("A" & iRow + 1).Value = RefNo
        End If
        TextBox1.Value = WorksheetFunction.Max(Range("Customers!A8:A65536")) + 1
    End Sub
    
    Private Sub Addreccord_Click()
        Dim LastRow As Object
    
        Set LastRow = Range("Customers!A65536").End(xlUp)
    
        LastRow.Offset(1, 0).Value = WorksheetFunction.Max(Range("Customers!A8:A65536")) + 1
        LastRow.Offset(1, 1).Value = TextBox2.Text
        LastRow.Offset(1, 2).Value = TextBox3.Text
        LastRow.Offset(1, 3).Value = TextBox4.Text
        LastRow.Offset(1, 4).Value = TextBox5.Text
        LastRow.Offset(1, 5).Value = TextBox6.Text
        LastRow.Offset(1, 6).Value = TextBox7.Text
        LastRow.Offset(1, 7).Value = TextBox8.Text
        LastRow.Offset(1, 8).Value = TextBox9.Text
        LastRow.Offset(1, 9).Value = TextBox10.Text
        LastRow.Offset(1, 10).Value = TextBox11.Text
    
        MsgBox "One record added to Customers List"
    
        response = MsgBox("Do you want to enter another record?", _
                  vbYesNo)
    
        If response = vbYes Then
            TextBox1.Value = WorksheetFunction.Max(Range("Customers!A8:A65536")) + 1
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
            TextBox11.Text = ""
    
            TextBox2.SetFocus
    
        Else
           Unload Me
        End If
    
    End Sub
    Private Sub Exitform_Click()
        End
    End Sub
    Sub ClearFields_Click()
        For Each ctrl In Me.Controls
            Select Case TypeName(ctrl)
                Case "TextBox"
                    ctrl.Text = ""
            End Select
        Next ctrl
    End Sub
    If my spreadsheet is required, please let me know and I can post it here

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    Try something like this. You can decide how to best integrate it with your existing code.

        Dim LastRow As Range, strNextID As String
        
        Set LastRow = Range("Customers!A65536").End(xlUp)
        strNextID = "AA-" & Format(Right(LastRow.Value, 5) + 1, "00000")      'Last ID + 1  ID format = AA-00000
        
        MsgBox "One record added to Customers List" & vbLf & "New Customer ID is " & strNextID
    Also note: it's not good practice to close a userform with the End command. That will abruptly quit all VBA code execution and could cause unexpected consequences. It's more common to use Unload Me instead.

    Private Sub Exitform_Click()
        Unload Me  'Don't use the 'End' command to quit a userform
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    02-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    22

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    @AlphaFrog, thanks for your suggestion about "Unload Me", it is always good to learn something new.

    The firs part part of the code did not work at all, possible reason is that I might have done something that I should not have.

    If it is not too much can you please post entire code as it should be?

    Thanks in advance.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    That is a lot to ask and I'm a bit lazy. I don't want to have to build my own userform and data sheet and then guess if it's like yours. Can you post an example workbook? Help me help you.

    Also; your ID naming convention (TAS1) in the UserForm_Activate procedure doesn't match your description (AA-12345); explain?

    It's a given if it didn't work, one of us did something wrong. Just saying it doesn't work without explanation of what you tried and what then happened doesn't help me diagnose the problem. It may be something as simple as a misspelled or misplaced command. You really need to be much more verbose. Take the time to fully describe what you want, what you tried, and what was the result of the attempt.

  5. #5
    Registered User
    Join Date
    02-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    22

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    I've posted code that I was trying at that point of time by accident. Here I am posting another version of the code and Image of the Customer DB where Data posted via UserForm is stored.

    Private Sub UserForm_Activate()
        Dim LastRow As Range, strNextID As String
        
        Set LastRow = Range("Customers!A65536").End(xlUp)
        
        strNextID = "AA-" & Format(Right(WorksheetFunction.Max(Range("Customers!A8:A65536")), 5) + 1, "00000")
        
        TextBox1.Value = strNextID
        Dim v, e
        With Sheets("Settings").Range("_paymentTerms")
            v = .Value
        End With
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For Each e In v
                If Not .exists(e) Then .Add e, Nothing
            Next
            If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
        End With
        
    End Sub
    Private Sub Addreccord_Click()
        Dim LastRow As Range, strNextID As String
        
        Set LastRow = Range("Customers!A65536").End(xlUp)
        
        strNextID = "AA-" & Format(Right(WorksheetFunction.Max(Range("Customers!A8:A65536")), 5) + 1, "00000")
        
        LastRow.Offset(1, 0).Value = strNextID
        LastRow.Offset(1, 1).Value = TextBox2.Text
        LastRow.Offset(1, 2).Value = TextBox3.Text
        LastRow.Offset(1, 3).Value = TextBox4.Text
        LastRow.Offset(1, 4).Value = TextBox5.Text
        LastRow.Offset(1, 5).Value = TextBox6.Text
        LastRow.Offset(1, 6).Value = TextBox7.Text
        LastRow.Offset(1, 7).Value = TextBox8.Text
        LastRow.Offset(1, 8).Value = TextBox9.Text
        LastRow.Offset(1, 9).Value = TextBox10.Text
        LastRow.Offset(1, 10).Value = TextBox11.Text
        LastRow.Offset(1, 11).Value = ComboBox1.Value
    
        MsgBox "One record added to Customers List"
    
        response = MsgBox("Do you want to enter another record?", _
                  vbYesNo)
    
        If response = vbYes Then
            TextBox1.Value = strNextID
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
            TextBox11.Text = ""
    
            TextBox2.SetFocus
    
        Else
           Unload Me
        End If
    
    End Sub
    Private Sub Exitform_Click()
        Unload Me
    End Sub
    Sub ClearFields_Click()
        For Each ctrl In Me.Controls
            Select Case TypeName(ctrl)
                Case "TextBox"
                    ctrl.Text = ""
                Case "ComboBox", "ListBox"
                    ctrl.ListIndex = -1
            End Select
        Next ctrl
    End Sub
    Attached Images Attached Images

  6. #6
    Registered User
    Join Date
    02-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    22

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    No matter how many times I try to enter a new customer details ID remains the same when realistically it should change every time, I cannot figure out what I am doing wrong. Please help and thanks for being patient

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    Replace this...
    strNextID = "AA-" & Format(Right(WorksheetFunction.Max(Range("Customers!A8:A65536")), 5) + 1, "00000")
    With what I suggested.
    strNextID = "AA-" & Format(Right(LastRow.Value, 5) + 1, "00000")      'Last ID + 1  ID format = AA-00000

  8. #8
    Registered User
    Join Date
    02-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    22

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    Sweet, works like magic, but little too slow, otherwise perfect

    Thanks a lot and once again thank for being patient!!

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    Quote Originally Posted by Alexb77 View Post
    Sweet, works like magic, but little too slow, otherwise perfect
    Are you talking about the code I posted?

    I have tested the code for myself with 47000 rows of data and took only 0.109375 sec.

    It must be other part of your code....

  10. #10
    Registered User
    Join Date
    02-08-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    22

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    I have done so but it did not work, I ended with and Error

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: How to implement Customer ID auto increment when using alpha numeric values with VBA

    Try
    strNextID = Format$(Sheets("customers").[max(if(a1:a65536<>"",mid(a1:a65536,4,5)+0))] + 1, "AA-00000")

+ 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. Replies: 9
    Last Post: 11-18-2013, 07:40 AM
  2. Macro to delete alpha and alpha numeric values
    By vijanand1279 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-20-2011, 09:13 AM
  3. Extracting Numeric Values from an Alpha/Numeric String
    By Delkath in forum Excel General
    Replies: 5
    Last Post: 10-27-2010, 02:36 PM
  4. Increment Alpha Numeric Cell
    By Theresa in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-08-2006, 02:45 PM
  5. Increment Alpha Numeric Cell
    By Theresa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-06-2006, 05:40 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