-
Random Selection of Data
I can randomly select data depending on the column that the user enters on the InputBox that comes up. What I really want to do is instead of the user copying and pasting the data to select the random data from to the sheet with the macro is to be able to just open the sheet with the macro and run the macro from the other workbook which I know how to do by going to Macro and selecting from All Open Workbooks. What I can't get to work is for it to run on the active workbook.
Here is my code:
Sub RandomStudentIDs()
Dim r As Range, i As Long
Dim myNum
myNum = Application.InputBox("Enter the desired number of random data rows")
myColumn = Application.InputBox("Enter the column to select the random data from")
myResults = Application.InputBox("Enter the column to put the random data results in")
ActiveWorkbook.Activate
myRange = "" & myColumn & "3"
With CreateObject("System.Collections.SortedList")
Randomize
For Each r In Range(myRange, Range(myColumn & Rows.Count).End(xlUp))
.Item(Rnd) = Array(r(, 0).Value, r.Value)
Next
Columns("i").ClearContents
For i = 0 To Application.Min(myNum - 1, .Count - 1)
Cells(i + 3, "" & myResults & "").Resize(, 2).Value = .GetByIndex(i)
Next
End With
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules