Consulting

Results 1 to 12 of 12

Thread: Automatic random code pattern

  1. #1

    Automatic random code pattern

    hellow
    I need to create VBA code to create a random code automatic (based on default -> TES001 ... TES145 and ARQ001 ... ARQ145 etc.).


    I have a tab "Tb_Rnd" which would crado codes via VBA.


    But I need not repeat codes.
    Aupha-Random-Automatic-Used
    TES001
    ARQ001

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why VBA, why not just a formula such as

    =CHOOSE(RANDBETWEEN(1,2),"TES","ARQ")&TEXT(RANDBETWEEN(1,145),"000")
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    interesting, but I need a way to compare "Used" tab with the "Tb_Rnd"


    Used happens that tab enter an alphanumeric based on "Tb_Rnd" tab but can not repeat.
    Tab "Used"
    ID's Descr
    TES001 Plano de teste
    TES002 Fluxo de integração
    TES003 Acompanhamento de execução
    ARQ004 Documento de arquitetura
    ARQ005 DAS
    ARQ006 DASW
    TES001 <-- it Code TES001 can not get into A10, becouse repeat in "Tb_Rnd"
    Tab "Tb_Rnd"
    Rand
    TES001
    TES0061
    ARQ0248
    ARQ0168
    ARQ0275
    TES0883
    ARQ0624
    ARQ0175

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    So now I am confused as to what the question is.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    I will use your formula in the "Tb_Rnd" tab and the "Used" tab will be in accordance with a click of a button station, but it can only be called the ID is not repeated in the "Used" tab

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Sorry, but I just don't understand.

    The phrase ... Used happens that tab enter an alphanumeric based on "Tb_Rnd" tab but can not repeat... is beyoind my comprehension.

    And this ... and the "Used" tab will be in accordance with a click of a button station, but it can only be called the ID is not repeated in the "Used" tab ... also is beyond me.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    i'm try this, but not apear nothing in active cell
    Sub Not_It_Work()  Worksheets("Used").Activate
      With ActiveCell
        .Formula = "=INDEX(Tb_Rnd!$A$2:$A$1000,RANDBETWEEN(0,COUNTA(Tb_Rnd!$A$2:$A$1000)))"
        .Value = .Range("A2").Value
      End With
    End Sub

  8. #8
    hellow
    Now I need to find the value in the active cell and delete this value "Tb_Rnd" tab
    How do I do that?
    Sub Now_It_Work()  Worksheets("Used").Activate
      With ActiveCell
        .Formula = "=INDEX(Tb_Rnd!$A$2:$A$1000,RANDBETWEEN(0,COUNTA(Tb_Rnd!$A$2:$A$1000)))"
        .Value = .Value '<- this line was wrong, but I fix
      End With
      'Now I need to find the value in the active cell and delete this value "Tb_Rnd" tab
      'How do I do that?
      'Thank you!
    End Sub
    Thank you!

  9. #9
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    Quote Originally Posted by elsg View Post
    Now I need to find the value in the active cell and delete this value "Tb_Rnd" tab
    hi.

    try this.

    Sub find_n_del()
    
    
        Dim rng2del As Range
        
        On Error Resume Next
        Set rng2del = Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlWhole, xlByRows, xlNext).Clear 'match entire cell contents
        'Set rng2del = Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlPart, xlByRows, xlNext).Clear 'partial match
        If Not rng2del Is Nothing Then
            rng2del.Clear
        Else
            MsgBox "Searched value not found!", vbOKOnly, "Not Found"
        End If
        On Error GoTo 0
    
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  10. #10
    Funny, the code reports by a msgbox that did not match, but in "Tb_Rnd" tab has been deleted by the code.


    how to fix, this MsgBox?


    Thank you!

  11. #11
    I try this
    Sub find_n_del()    Dim rng2del As Range
        On Error Resume Next
        Set rng2del = Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlWhole, xlByRows, xlNext).Clear 'match entire cell contents
         'Set rng2del = Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlPart, xlByRows, xlNext).Clear 'partial match
        If Not rng2del Is Nothing Then
            rng2del.Clear
        On Error GoTo 0
         End If
         With Worksheets("Tb_Rnd").Activate
           Columns("A:A").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes
           Columns("B:B").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes
        End With
        Worksheets("Used").Activate
    End Sub
    Thank you!!!

  12. #12
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    ooppps.

    sory for that.

    my first code was
    Sub find_n_del()
        Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlWhole, xlByRows, xlNext).Clear 'match entire cell contents
        'Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlPart, xlByRows, xlNext).Clear 'partial match
    End Sub
    thinking that ActiveCell's value always exists in "Tb_Rnd".

    but what if there is not a match... so i added error handling. but i forgot to delete the "Clear"!



    correct procedure must be like this:
    Sub find_n_del()
    
    
        Dim rng2del As Range
        
        On Error Resume Next
        Set rng2del = Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlWhole, xlByRows, xlNext) 'match entire cell contents
        'Set rng2del = Worksheets("Tb_Rnd").Cells.Find(ActiveCell.Value, , , xlPart, xlByRows, xlNext) 'partial match
        If Not rng2del Is Nothing Then
            rng2del.Clear
        Else
            MsgBox "Searched value not found!", vbOKOnly, "Not Found"
        End If
        On Error GoTo 0
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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