bs_user
asked on
How can I select random rows in excel for auditing
We need to do some auditing in excel and what we require is 50 random rows to be selected from 900 records.
Is this possible? if so how do we go about this?
Many Thanks
Is this possible? if so how do we go about this?
Many Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Where would I enter that code Cluskitt, im a bit of a newb when it comes to excel
bs user,
what is your expected outcome? I read your question that you wanted a list of 50 random numbers between 1 and 900. Why VBA? Can you explain?
cheers, teylyn
what is your expected outcome? I read your question that you wanted a list of 50 random numbers between 1 and 900. Why VBA? Can you explain?
cheers, teylyn
ASKER
I want to select 50 random rows which contain data.
Say for example we have 2100 rows of data. We need to audit 50 rows selected by random, when I tested your code it just created random numbers.
Hope that makes a bit more sense, sorry if my explaination is confusing.
Many Thanks
Say for example we have 2100 rows of data. We need to audit 50 rows selected by random, when I tested your code it just created random numbers.
Hope that makes a bit more sense, sorry if my explaination is confusing.
Many Thanks
This is VBA code. It depends on what you want to do. If all you want is to get 50 values and audit the rows manually, then teylyn's solution is good enough. If you want a more complete solution, you can use VBA to, for example, highlight the selected rows. Or to copy those rows into a new sheet. For example, this will create a copy of 50 rows from sheet1 to sheet2:
Sub AuditRows()
Dim arrAuditRows[1 to 50] As Integer
For i=1 To 50
arrAudit(i)=Int ((900 - 1 + 1) * Rnd + 1)
For a=1 To i
If arrAudit(i)=arrAudit(a) Then
arrAudit(i)=0
End If
Next
If arrAudit(i)=0 Then
i=i-1
End If
Next
For i=1 to 50
Sheets(1).Cells(arrAudit(i ), 1).EntireRow.Copy
Sheets(2).Cells(i, 1).EntireRow.Paste
Next
End Sub
To use this macro, you can access the VBA editor (Alt+F11), create a module (on the left side, in the project explorer, right click->Insert->Module) and paste this code there. Then, in excel, View->Macros->View Macros. Select this macro, chose options and assign a key to the shortcut. You can then use the shortcut to run it at will.
Sub AuditRows()
Dim arrAuditRows[1 to 50] As Integer
For i=1 To 50
arrAudit(i)=Int ((900 - 1 + 1) * Rnd + 1)
For a=1 To i
If arrAudit(i)=arrAudit(a) Then
arrAudit(i)=0
End If
Next
If arrAudit(i)=0 Then
i=i-1
End If
Next
For i=1 to 50
Sheets(1).Cells(arrAudit(i
Sheets(2).Cells(i, 1).EntireRow.Paste
Next
End Sub
To use this macro, you can access the VBA editor (Alt+F11), create a module (on the left side, in the project explorer, right click->Insert->Module) and paste this code there. Then, in excel, View->Macros->View Macros. Select this macro, chose options and assign a key to the shortcut. You can then use the shortcut to run it at will.
What do you mean by "I want to select"?
Select and then what? Just select 50 rows? Or do you want to do something with them? Do you want the rows to be highlighted or change color or "select" them in the context of a macro that does some further manipulation?
You need to be a bit more descriptive with your requirements, please.
Select and then what? Just select 50 rows? Or do you want to do something with them? Do you want the rows to be highlighted or change color or "select" them in the context of a macro that does some further manipulation?
You need to be a bit more descriptive with your requirements, please.
If you don't know how many rows you have, you can add:
Dim arrAuditRows[1 to 50], MaxRow As Integer
MaxRow=Cells.Find(What:="* ", After:=[A1], SearchDirection:=xlPreviou s).Row
And change:
arrAudit(i)=Int ((900 - 1 + 1) * Rnd + 1)
to
arrAudit(i)=Int ((Maxrow - 1 + 1) * Rnd + 1)
This will find the very last cell that has a value, and read it as the last row.
Also, if you have a header, You have to change the 1's with 2's (or appropriate number for first row).
Dim arrAuditRows[1 to 50], MaxRow As Integer
MaxRow=Cells.Find(What:="*
And change:
arrAudit(i)=Int ((900 - 1 + 1) * Rnd + 1)
to
arrAudit(i)=Int ((Maxrow - 1 + 1) * Rnd + 1)
This will find the very last cell that has a value, and read it as the last row.
Also, if you have a header, You have to change the 1's with 2's (or appropriate number for first row).
ASKER
Thanks for the info
cheers, teylyn