Generate 5000 records in 2 columns of random number that being unique

后端 未结 3 2035
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 04:14

How I can generate 5000 records in 2 columns of random numbers between 1 and 100 that being unique.

For example:

 A            B
----------------
 1          


        
3条回答
  •  执笔经年
    2020-12-04 04:53

    First run this tiny macro:

    Sub dural()
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        k = 1
        For i = 1 To 100
            For j = 1 To 100
                Cells(k, 1) = i
                Cells(k, 2) = j
                Cells(k, 3).Formula = "=rand()"
                k = k + 1
            Next j
        Next i
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = True
    End Sub
    

    Then sort cols A,B,C by column C.
    Then pick the first 5000 rows.

    enter image description here


    EDIT#1:

    To remove cases in which the value in column A is the same as the value in column B use this macro instead:

    Sub dural()
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        k = 1
        For i = 1 To 100
            For j = 1 To 100
                If i <> j Then
                    Cells(k, 1) = i
                    Cells(k, 2) = j
                    Cells(k, 3).Formula = "=rand()"
                    k = k + 1
                End If
            Next j
        Next i
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = True
    End Sub
    

提交回复
热议问题