+ Reply to Thread
Results 1 to 10 of 10

unique-string generator

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Question unique-string generator

    Hi,

    I was wondering if any of you has already created a unique-string generator
    it cannot be just using date & time as it needs to work during run-time

    I figured out that I can just increment an integer, but I was wondering if any of you has code to do the same in alphanumeric fashion (or just letters, for that matter), to get a shorter id

    .. X,Y,Z,AA,AB,AC ... AZ, BA, BB, ... , ZZ, AAA, AAB, ... etc

    i want to use them in shape names and so on, so the shorter the better

    thanks!

  2. #2
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Re: unique-string generator

    If Alpha numeric is good, then why not increment A1? It wont change the Letter A but will give you Alpha numeric unique strings..
    Cheers!
    Deep Dave

  3. #3
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: unique-string generator

    Quote Originally Posted by bagullo View Post
    it cannot be just using date & time as it needs to work during run-time
    Can you elaborate more on this? What is the problem using time-stamp
    On the other hand if it's shape names, it could be useful just to start with same common letters and then increment index number...
    If you are pleased with a member's answer then use the Star icon to rate it.

  4. #4
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Re: unique-string generator

    Quote Originally Posted by buran View Post
    Can you elaborate more on this? What is the problem using time-stamp
    On the other hand if it's shape names, it could be useful just to start with same common letters and then increment index number...
    Hi Buran,
    then thing is that if I write two rows on run-time, 2 of them may be written in the same second, and thus ID is not unique anymore.
    Regarding letters + number, it's one of the ways I am doing it already, I was just wondering about an elegant-looking piece of code to convert strings to number and vice-versa to increment them, in the same way of columns names in excel. (.. AZ, BA, BB, ... )

    Quote Originally Posted by NeedForExcel View Post
    If Alpha numeric is good, then why not increment A1? It wont change the Letter A but will give you Alpha numeric unique strings..
    what do you mean ? incement cell A1? or rather combine letter plus number as buran suggests?

    Thanks!
    Last edited by bagullo; 04-14-2015 at 07:27 AM. Reason: double quoted the same message

  5. #5
    Forum Expert
    Join Date
    06-25-2009
    Location
    Sofia, Bulgaria, EU
    MS-Off Ver
    Excel 2003-2013
    Posts
    1,290

    Re: unique-string generator

    I see, I would definitely go for some common string + index number e.g. shp1, shp2, etc.
    Depending on what you are doing it may turn out to be useful to loop trough all shapes (somewhere in the code) and just check what its name starts with and do something

  6. #6
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Re: unique-string generator

    what do you mean ? incement cell A1? or rather combine letter plus number as buran suggests?
    Yes Exactly! You can do that..

  7. #7
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: unique-string generator

    If you mean to increment from cell A1, then here's an example:
    Sub test()
        Dim i As Long
        For i = 1 To 100
            Debug.Print Split(Cells(1, i).Address(0, 0), 1)(0)  'code here
        Next i
    End Sub
    debug.print shown in the Immidiate window (Alt+G)

  8. #8
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Re: unique-string generator

    I am saying something like this. (Berlan's Code Edited)

    Sub test()
        Dim i As Long
        For i = 1 To 100
            Debug.Print "Shape" & i 'code here
        Next i
    End Sub

  9. #9
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Re: unique-string generator

    thanks berlan & Needforexcel

  10. #10
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Smile Re: unique-string generator

    Well --

    finally I did myself the piece of code I whished for. Not quite elegent, but it certainly does work.
    i did it to work just with letters, but changing 26 for whatever number you want, and modifying the mapping funcions it can work as a hexadecimal conversor or change the base to whatever suits you

        Dim wsf As WorksheetFunction
    
    '2015-04-14 / B.Agullo /
    Public Function numberToLetters(ByVal n As Long) As String
    'description here
     
        Dim m As Long 'mod
        Dim i As Long 'div
        
        numberToLetters = ""
        
        Set wsf = Application.WorksheetFunction
        
        m = numMod(n, 26)
        i = wsf.RoundDown(n / 26, 0)
        numberToLetters = numberToLetters & numberToLetter(m)
        
        While i > 26
            m = numMod(i, 26)
            i = wsf.RoundDown(i / 26, 0)
            numberToLetters = numberToLetter(m) & numberToLetters
        Wend
        
        numberToLetters = numberToLetter(i) & numberToLetters
        
    
        Set wsf = Nothing
    End Function
    
    
    '2015-04-14 / B.Agullo /
    Public Function lettersToNumber(ByVal s As String) As Long
    'description here
        
        Dim i As Integer
        Dim chrNum As Integer
        Dim unitValue As Long
        Dim chrV As Long
        Dim chrS As String
        
        
        chrNum = Len(s)
        
        lettersToNumber = 0
        
        For i = chrNum To 1 Step -1
            unitValue = 26 ^ (i - 1)
            chrS = Mid(s, chrNum - i + 1, 1)
            chrV = letterToNumber(chrS)
            lettersToNumber = lettersToNumber + unitValue * chrV
            
        Next
     
    End Function
    
    
    '2015-04-14 / B.Agullo /
    Public Function numMod(ByVal myNum As Long, ByVal myDivisor As Long) As Long
    'description here
        
        
        numMod = myNum - myDivisor * (wsf.RoundDown(myNum / myDivisor, 0))
     
    End Function
    
    '2015-04-14 / B.Agullo /
    Public Function numberToLetter(ByVal n As Long) As String
    'n = 1 for A, 26 for Z
        
        numberToLetter = Chr(64 + n)
        
    End Function
    
    '2015-04-14 / B.Agullo /
    Public Function letterToNumber(ByVal s As String) As Long
    'n = 1 for A, 26 for Z
        
        letterToNumber = Asc(s) - 64
        
    End Function


    numbersToLetter.xlsm

+ 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. Need help removing all symbols from a random string generator...
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-12-2013, 03:48 PM
  2. [SOLVED] Use this unique ID generator macro to insert into a textbox in a userform
    By bg_enigma1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2012, 12:21 PM
  3. Unique Random Number generator
    By David Obeid in forum Excel General
    Replies: 3
    Last Post: 01-28-2008, 11:27 PM
  4. Count unique characters in a string
    By yelkus in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-25-2007, 11:46 AM
  5. Unique Password Generator
    By AllanWilliams in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-16-2007, 11:41 AM

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