VBA code to select & highlight x no. of rows randomly based on the categories in column

linspiredgal

New Member
Joined
Apr 13, 2012
Messages
25
Hi

I need random record to be selected from a group of categories in column.

Eg- from the table below- I need two names randomly highlighted, from each city type.
i.e two randomly selected names from NYC, seattle, toronto and california each.

NameClasscity
ABC1NYC
DEF2NYC
GHI2Seattle
JKL2Seattle
MNO3Toronto
PQR3Toronto
STU4Toronto
VWX5NYC
YZ5California
BVB2Toronto
GHG2Toronto
GDCK3NYC
HGHG3NYC
DFDF5California
DCBJ5California

<tbody>
</tbody>
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Re: I need VBA code to select & highlight x no. of rows randomly based on the categories in column

Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Jun18
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C2", Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn.Address
    [COLOR="Navy"]Else[/COLOR]
        .Item(Dn.Value) = .Item(Dn.Value) & "," & Dn.Address
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, Rd [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] nRd [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    Sp = Split(.Item(K), ",")
    Rd = Application.RandBetween(0, UBound(Sp))
    nRd = Rd
     [COLOR="Navy"]Do[/COLOR] Until nRd <> Rd
        nRd = Application.RandBetween(0, UBound(Sp))
     [COLOR="Navy"]Loop[/COLOR]
     Range(Sp(Rd)).EntireRow.Interior.Color = vbYellow
     Range(Sp(nRd)).EntireRow.Interior.Color = vbYellow
[COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Re: I need VBA code to select & highlight x no. of rows randomly based on the categories in column

Thanks a lot Mick.
It works perfect.

Could you also tell me where do I need to make change if I need more or less then 2 records from each city to be selected?

Regards
 
Last edited:
Upvote 0
Re: I need VBA code to select & highlight x no. of rows randomly based on the categories in column

Try this:-
NB:- When you run the code , You will be asked for a number, this is the number of rows to Highlight for any given state/city, if the number entered is Greater than the count of any individual state then all entries for that that state/city will all be highlighted.

Code:
[COLOR="Navy"]Sub[/COLOR] MG18Jun12
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Num [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C2", Range("C" & Rows.Count).End(xlUp))
Num = InputBox("Enter number", "Number", "Enter your number here")
[COLOR="Navy"]If[/COLOR] Num < 1 Or Not IsNumeric(Num) [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]

[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn.Address
    [COLOR="Navy"]Else[/COLOR]
        .Item(Dn.Value) = .Item(Dn.Value) & "," & Dn.Address
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]


[COLOR="Navy"]Dim[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, Rd [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant, nStr [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] SpA [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    Sp = Split(.Item(K), ",")
    [COLOR="Navy"]If[/COLOR] Num >= UBound(Sp) + 1 [COLOR="Navy"]Then[/COLOR]
        Range(.Item(K)).EntireRow.Interior.Color = vbYellow
    [COLOR="Navy"]Else[/COLOR]
        Do
            Rd = Application.RandBetween(0, UBound(Sp))
            [COLOR="Navy"]If[/COLOR] InStr(nStr, Rd) = 0 [COLOR="Navy"]Then[/COLOR]
               nStr = nStr & IIf(nStr = "", Rd, "," & Rd)
            [COLOR="Navy"]End[/COLOR] If
        Loop Until UBound(Split(nStr, ",")) + 1 = Num
     
        SpA = Split(nStr, ",")
        [COLOR="Navy"]For[/COLOR] n = 0 To UBound(SpA)
            Range(Sp(SpA(n))).EntireRow.Interior.Color = vbYellow
        [COLOR="Navy"]Next[/COLOR] n
            nStr = ""
    [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Re: I need VBA code to select & highlight x no. of rows randomly based on the categories in column

Thanks Mick.
It works.. :)
 
Last edited:
Upvote 0
Re: I need VBA code to select & highlight x no. of rows randomly based on the categories in column

You're welcome
 
Upvote 0

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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