VBA CODE TO AUTO GENERATE UNIQUE ID

vba programming

New Member
Joined
Jan 12, 2023
Messages
8
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi! Please I seriously need your help in creating a unique ID for members in the form;
SAC-01
SAC-02
SAC-03. In that order
In worksheets(“MEMBER DETAILS”) at row 2 column B
The name Manager for the table is (Mlist)
Please I would want it to happen Automatically
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try This
VBA Code:
Sub GenerateID()
    Dim i As Integer
    i = 1

    'Loop through the Mlist table
    Do Until IsEmpty(Range("Mlist[ID]"))
        i = i + 1
    Loop

    'Insert the ID in the specified cell
    Worksheets("MEMBER DETAILS").Range("B2").Value = "SAC-" & Format(i, "00")
End Sub

This code assumes that the table "Mlist" has a column named "ID". The code will loop through the "ID" column and find the last used ID, increment it by 1 and insert the new ID in the specified cell "B2" on the "MEMBER DETAILS" worksheet.

You can run the subroutine GenerateID() whenever you want to generate a new ID.

You can also call this subroutine in a button click or any other event that you want to use to generate the ID.
 
Upvote 0
Please where ex
Try This
VBA Code:
Sub GenerateID()
    Dim i As Integer
    i = 1

    'Loop through the Mlist table
    Do Until IsEmpty(Range("Mlist[ID]"))
        i = i + 1
    Loop

    'Insert the ID in the specified cell
    Worksheets("MEMBER DETAILS").Range("B2").Value = "SAC-" & Format(i, "00")
End Sub

This code assumes that the table "Mlist" has a column named "ID". The code will loop through the "ID" column and find the last used ID, increment it by 1 and insert the new ID in the specified cell "B2" on the "MEMBER DETAILS" worksheet.

You can run the subroutine GenerateID() whenever you want to generate a new ID.

You can also call this subroutine in a button click or any other event that you want to use to generate the ID.
Please where exactly should I write the codes please
 
Upvote 0
To paste code into a new macro in Excel, follow these steps:

  1. Open Excel and the workbook in which you want to create the macro.
  2. Click on the "Developer" tab in the ribbon. If you don't see the "Developer" tab, you can enable it by going to the "File" menu, then "Options," then "Customize Ribbon" and checking the "Developer" box.
  3. In the "Code" group, click on the "Visual Basic" button to open the Visual Basic Editor.
  4. In the Project window, right-click on Microsoft Excel Object and select "Insert" then "Module"
  5. In the module window, paste your code.
  6. Save the macro by clicking "File" then "Save"
  7. Close the Visual Basic Editor by clicking the "X" on the top right corner
  8. To run the macro, you can either press the keyboard shortcut you assigned earlier or go to the "Developer" tab and click on the "Macros" button to select the macro from the list and click "Run".
 
Upvote 0
Please where ex

Please where exactly should I write the codes please
To paste code into a new macro in Excel, follow these steps:

  1. Open Excel and the workbook in which you want to create the macro.
  2. Click on the "Developer" tab in the ribbon. If you don't see the "Developer" tab, you can enable it by going to the "File" menu, then "Options," then "Customize Ribbon" and checking the "Developer" box.
  3. In the "Code" group, click on the "Visual Basic" button to open the Visual Basic Editor.
  4. In the Project window, right-click on Microsoft Excel Object and select "Insert" then "Module"
  5. In the module window, paste your code.
  6. Save the macro by clicking "File" then "Save"
  7. Close the Visual Basic Editor by clicking the "X" on the top right corner
  8. To run the macro, you can either press the keyboard shortcut you assigned earlier or go to the "Developer" tab and click on the "Macros" button to select the macro from the list and click "Run".
Please I mean should I past the codes at Thisworkbook or at the MEMBER DETAILS sheet
 
Upvote 0
Perhaps my descriptions were not clear ….
I mean I need your help in generating a unique ID for members In the form SAC-01 …

The textbook that will hold the ID name is text box 1 and it will fetch the ID number to MEMBER DETAILS sheet range B2

I would want the ID to be auto generated …. Immediately the log in form is triggered to enter data to MEMBER DETAILS sheet..


The code didn’t work for me 🥺
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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