Random Place Name Generator

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Here is a macro modeled on that logic from that workbook.

Select the columns as a single rectangle (with the values at top, any trailing blanks will be ignored) and run the macro. The output will be one column to the right of those cells. Also, be cognizant that the sorting uses/clears an additional column to the right of the output (column D in your example).

Edit: Just realized I may have misinterpreted the meaning of 'random' in this context. Comment out the last two lines if you don't want the order of the results randomized.

Code:
Option Explicit
Sub RandomlyOrderedCategoryCombinations()

    Dim r As Range
    Dim cl As Long, rw As Long
    Dim arr As Variant, cumCombinations As Variant, cntCategory As Variant
    
    
    Set r = Selection
    cl = r.Columns.Count
    ReDim cumCombinations(1 To cl)
    ReDim cntCategory(1 To cl)
    
    Do While cl >= 1
        cntCategory(cl) = Application.WorksheetFunction.CountA(r.Columns(cl))
        
        If cl <> r.Columns.Count Then
            cumCombinations(cl) = cumCombinations(cl + 1) * cntCategory(cl)
        Else
            cumCombinations(cl) = cntCategory(cl)
        End If
        
        cl = cl - 1
    Loop
    
    Dim i As Long, j As Long
    ReDim arr(1 To cumCombinations(1), 1 To 2)
    
    For i = 1 To cumCombinations(1)
        For j = 1 To r.Columns.Count
            arr(i, 1) = arr(i, 1) & r.Cells(Int((i - 1) * cntCategory(j) / cumCombinations(j)) Mod cntCategory(j) + 1, j) & " "
        Next j
        
        arr(i, 1) = Trim(arr(i, 1))
        arr(i, 2) = Rnd
    Next i

    Set r = r.Offset(, r.Columns.Count).Resize(UBound(arr, 1), 2)
    r.Value = arr
    r.Sort Key1:=r.Offset(, 1).Resize(1, 1)
    r.Columns(2).Clear

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,896
Members
452,948
Latest member
Dupuhini

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