Looking to expand a VBA Macro / Pick random cell from column

peejaygee

New Member
Joined
Jul 23, 2006
Messages
40
Hey All,

So, I have a list of items in a column, that I'd like to randomly pick from.

I have a very basic routine, that picks on, and highlights the column a based on what it picks. But,

I'd like to be able to randomly pick, but if it's already been picked before to not pick it again, I'm guessing by checking the colour of the cell?

At the moment, I have this

Sub FindRandomCell()
Dim ColumnA As Long
Dim StartRow As Long
Dim HeaderRow As Long
Dim LastRow As Long
Dim randomNum As Long
ColumnA = 1
HeaderRow = 1
StartRow = 3
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
randomNum = WorksheetFunction.RandBetween(StartRow, LastRow)
Cells(randomNum, ColumnA).Interior.Color = RGB(0, 255, 0)
Cells(randomNum, ColumnA).Select
End Sub

Any thoughts would be appreciated.

Thanks.
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG27Mar41
[COLOR="Navy"]Dim[/COLOR] Lastrow [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] RandomNum [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
RandomNum = WorksheetFunction.RandBetween(3, Lastrow)
DoEvents
[COLOR="Navy"]Do[/COLOR] [COLOR="Navy"]While[/COLOR] Cells(RandomNum, "A").Interior.Color = RGB(0, 255, 0)
    RandomNum = WorksheetFunction.RandBetween(3, Lastrow)
    c = c + 1
    [COLOR="Navy"]If[/COLOR] c > Lastrow [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Loop[/COLOR]
 
Cells(RandomNum, "A").Interior.Color = RGB(0, 255, 0)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thank you, that did the trick.

I added in Cells(randomNum, "A").Select so it jumps to the cell and it's now perfect. :)
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top