Consulting

Results 1 to 4 of 4

Thread: generate random and unique value of cells

  1. #1
    VBAX Regular
    Joined
    Sep 2017
    Posts
    14
    Location

    generate random and unique value of cells

    hi all, i have values in column and i want to generate 5 value for random and without duplicate when i click on button with vba, my example file :

    thanks in advanced,,,,,sorry for my poor english.
    Attached Images Attached Images

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()    Set r = ActiveSheet.UsedRange.Columns(1)
        r.Copy r.Offset(, 2)
        r.Offset(, 2).SpecialCells(2).Offset(, 1).Formula = "=Rand()"
        Range("C:D").Sort Key1:=Range("D:D"), Order1:=xlAscending, Header:=xlNo
        Range("C6:D" & Rows.Count).ClearContents
        Range("D:D").ClearContents
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    To avoid reporting duplicates, I think you need to add the marked line.

    I added the .ScreenUpdating=False because the flashing was annoying me


    Option Explicit
    Sub Test1()
        Dim R As Range
        
        Application.ScreenUpdating = False
    
        Set R = ActiveSheet.UsedRange.Columns(1)
        R.Copy R.Offset(, 2)
        R.Offset(, 2).SpecialCells(2).Offset(, 1).Formula = "=Rand()"
        Range("C:D").Sort Key1:=Range("D:D"), Order1:=xlAscending, Header:=xlNo
    
        'added
        Range("C:D").RemoveDuplicates Columns:=1, Header:=xlNo
        
        Range("C6:D" & Rows.Count).ClearContents
        Range("D:D").ClearContents
    
        Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Sep 2017
    Posts
    14
    Location
    thank you very much for your help.

Posting Permissions

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