create unique ID like a number or number letter combo for each new entry

rjmdc

Well-known Member
Joined
Apr 29, 2020
Messages
672
Office Version
  1. 365
Platform
  1. Windows
what vba would i use to add a unique transaction ID as soon as first column in the row is filled in. this id needs to be unchanging no matter what i do to this row
it also needs to stay static even if i sort or group.
please advise and help.
also this sheet already has many other vba so where would i add it
 

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.
.
The following will generate a UNIQUE ID (then alphanumeric characters). It is presently set for cell A1. That can be
edited as required.

VBA Code:
Option Explicit

Sub farhadj()
If Sheet1.Range("A1").Value <> "" Then Exit Sub
  With Range("A1")
    .Formula = "=DecToBij(RANDBETWEEN(1727605,62193780), ""^1$3%5(7&9AbCdEfGhIjKlMnOpQrStUvWxYz"")" & _
               "&DecToBij(RANDBETWEEN(1727605,62193780), ""@2#4^6*8)0aBcDeFgHiJkLmNoPqRsTuVwXyZ"")"
    .Value = .Value
  End With

End Sub

Function DecToBij(iNum As Long, sSym As String) As String
 
  If iNum > 0 Then DecToBij = DecToBij((iNum - 1) \ Len(sSym), sSym) & _
     Mid(sSym, ((iNum - 1) Mod Len(sSym)) + 1, 1)
End Function
 
Upvote 0
this would increase and add a unique id as a field row is added?
i can sort and the number stays with the field?
i can delete rows and the numbers still stay?
explain please
"=DecToBij(RANDBETWEEN(1727605,62193780), ""^1$3%5(7&9AbCdEfGhIjKlMnOpQrStUvWxYz"")"
 
Upvote 0
sorry
how would i amend /add to your code, i cant get this to work for me

target = column A
if target <> ""
then continue on into your code to add id to column T
 
Upvote 0
In the Worksheet_Change event, make a call to the macro so it will run :

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
farhadj
End Sub


Change the Cell reference as required :

Sheet1.Range("A1").Value '<--- change to the cell you want the unique ID to appear.
 
Upvote 0
can anyone else help me
workbook has a code to backup on the workbook
then sheet 1 has 3 macros already each one when a different cell is activated
i need that as soon as i fill in a name in column A then column T needs to fill automatically with ID #
this needs to be a permanent ID number for this entry no matter how i change or manipulate or sort it
 
Upvote 0
.
Paste the following into a REGULAR MODULE :

VBA Code:
Option Explicit

Public Function CreateGUID(Optional IncludeHyphens As Boolean = True, _
                           Optional IncludeBraces As Boolean = False) _
                           As String
   
    Dim obj As Object
    Dim strGUID As String
   
    Set obj = CreateObject("Scriptlet.TypeLib")
   
    'Assign the raw GUID, minus the last two erroneous chars
    strGUID = Left(obj.GUID, Len(obj.GUID) - 2)
   
    'If IncludeHyphens is switched from the default True to False,
    'remove them from the GUID
    If Not IncludeHyphens Then
        strGUID = Replace(strGUID, "-", vbNullString, Compare:=vbTextCompare)
    End If
   
    'If IncludeBraces is switched from the default False to True,
    'leave those curly braces be!
    If Not IncludeBraces Then
        strGUID = Replace(strGUID, "{", vbNullString, Compare:=vbTextCompare)
        strGUID = Replace(strGUID, "}", vbNullString, Compare:=vbTextCompare)
    End If
   
    CreateGUID = strGUID
   
End Function

Sub Test()
Dim GUID As String
On Error Resume Next

    GUID = CreateGUID() '<~ default
   
    If ActiveCell.Column <> 2 Then
        'ActiveCell.Offset(0, 18).Value = ""
        Exit Sub
    Else
        If ActiveCell.Offset(0, -1) <> "" Then
            ActiveCell.Offset(0, 18).Value = GUID
        End If
    End If
   
End Sub


Paste the following in the SHEET LEVEL MODULE :

VBA Code:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Test
End Sub

In your previous posts, you failed to mention this :

" i need that as soon as i fill in a name in column A then column T needs to fill automatically with ID # "

That small bit of information made all the difference. Consider this in future posts.
 
Upvote 0
ok
please explain where these codes go, how would i use them
i have a workbook save code on the workbook tab
then on sheet 1 i have all these codes
Private Sub Worksheet_Change(ByVal Target As Range)
Macro1 Target 'event runs when cell in Column G or column H is changed
Macro2 Target 'event runs when cell in Column E is changed
Macro3 Target 'event runs when cell in Column C is changed
Macro4 Target 'event runs when cell in Column A is changed

then i use each sub for ex.
Private Sub Macro1(ByVal Target As Range)
 
Upvote 0

Forum statistics

Threads
1,215,111
Messages
6,123,152
Members
449,098
Latest member
Doanvanhieu

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