random selection of cell

abee4life

Active Member
Joined
Jan 27, 2005
Messages
319
Hi,

I had a VBA code for what I'm about to ask, but it has seems to have disappeared from my spreadsheet, and been unable to locate it now for couple days, so assuming I've deleted it by accident or something.

I have tried to find it again through this site, but have had major problems trying to do that too...

So here goes, I am asking if someone can create one for me please as I am a complete newbie when it comes to VBA.

I have a list of 100 names in column A, and would like it to randomly select 1 at a time, and remove it from that list, and place it into the first available cell in column B - so for example, the first selected would go into B1, then the next selected would be in B2 and so on.

I hope this is understandable.

Thanks in advance.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi there,

Try this while on the sheet which has the numbers starting from cell A1:

Code:
Option Explicit
Sub Macro2()

    'http://www.mrexcel.com/forum/showthread.php?641460-random-selection-of-cell

    Dim blnMyNumbers(100) As Boolean
    Dim lngCurrentNum As Long, _
        lngCounter As Long
    
    Randomize 'Without this, the numbers will always be displayed in the same 'random' order each time the macro is run.
    
    Application.ScreenUpdating = False
    
    Do Until lngCounter = 100
        lngCurrentNum = Int(Rnd * 100) + 1
        If blnMyNumbers(lngCurrentNum) = False Then
            lngCounter = lngCounter + 1
            Range("A" & lngCurrentNum).Cut _
                Range("B" & lngCounter)
            blnMyNumbers(lngCurrentNum) = True
        End If
    Loop
    
    Erase blnMyNumbers
    
    Application.ScreenUpdating = True

End Sub

Also, you may have lost the previous macro because the file was saved as a "xlsx" format where workbooks with macro(s) must be saved with a "xlsm" extension.

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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