Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the following to cross join two different ranges. It will handle ranges of any size and write the crossjoined combinations to a target sheet that you specify.</p> <p>In the example below, I have defined two named ranges: <code>newValues</code> and <code>fixedValues</code>. Both of these ranges are on <code>Sheet1</code>. I then loop through the ranges and write all combinations to <code>Sheet2</code>.</p> <pre><code>Sub CrossJoinMyRanges() Dim ws As Worksheet Dim newValues As Range Dim cell As Range Set ws = ThisWorkbook.Sheets("Sheet1") Set newValues = ws.Range("newValues") ' loop through the new values For Each cell In newValues Call ReplaceMe(cell.Value, ws) Next cell End Sub Sub ReplaceMe(replacement As String, ws As Worksheet) Dim fixedValues As Range Dim cell As Range Set fixedValues = ws.Range("fixedValues") ' outer loop through fixedValues For Each cell In fixedValues Call PrintReplacedValues(cell.Row, replacement) Next cell End Sub Sub PrintReplacedValues(rowNumber As Long, replacement As String) Dim wb As Workbook Dim src As Worksheet Dim tgt As Worksheet Dim fixedValues As Range Dim cell As Range Dim printMe As String Dim x As Long, y As Long Set wb = ThisWorkbook Set src = wb.Sheets("Sheet1") Set tgt = wb.Sheets("Sheet2") Set fixedValues = src.Range("fixedValues") y = 1 x = tgt.Range("A" &amp; tgt.Rows.Count).End(xlUp).Row + 1 ' inner loop through fixed values For Each cell In fixedValues ' replace the fixed value with the replacement ' if the loops intersect If cell.Row = rowNumber Then printMe = replacement Else ' otherwise keep the fixed value printMe = cell End If ' write to the target sheet tgt.Cells(x, y).Value = printMe y = y + 1 Next cell End Sub </code></pre> <p>There are a few similar questions with alternative solutions that you can also look into if my approach isn't what you were after:</p> <p><a href="https://stackoverflow.com/q/10692653/138938">Excel vba to create every possible combination of a Range</a></p> <p><a href="https://stackoverflow.com/q/5018793/138938">How can i obtain cartesian product like columns in excel?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload