Repeat one table for each item in another

Joined
Feb 25, 2020
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I have a two ranges likes below:

Range1

Product
CPQ
SalesforceIQ
Quip
MC_Advertising_Studio
MC_Social_Studio
DataCloud
DataCloud
Analytics
Commerce_Cloud
Heroku
DMP_Krux
Essentials


Range 2: Countries
Brazil
Sweden
UK
Ireland
Turkey
India
Philippines
China
Argentina
Russia


Output: Populate the same list of products for each country like below:
BrazilCPQ
BrazilSalesforceIQ
BrazilQuip
BrazilMC_Advertising_Studio
BrazilMC_Social_Studio
BrazilDataCloud
BrazilDataCloud
BrazilAnalytics
BrazilCommerce_Cloud
BrazilHeroku
BrazilDMP_Krux
BrazilEssentials
SwedenCPQ
SwedenSalesforceIQ
SwedenQuip
SwedenMC_Advertising_Studio
SwedenMC_Social_Studio
SwedenDataCloud
SwedenDataCloud
SwedenAnalytics
SwedenCommerce_Cloud
SwedenHeroku
SwedenDMP_Krux
SwedenEssentials
UKCPQ
UKSalesforceIQ
UKQuip
UKMC_Advertising_Studio
UKMC_Social_Studio
UKDataCloud
UKDataCloud
UKAnalytics
UKCommerce_Cloud
UKHeroku
UKDMP_Krux
UKEssentials
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi *chaithanyakrishnagck,

Welcome to MrExcel!!

Try this:

VBA Code:
Option Explicit
Sub Macro2()

    Dim rngProduct As Range
    Dim rngCountry As Range
    Dim wsOutput As Worksheet
    Dim lngOutputRow As Long
    
    'Loop through each item in named ranges called 'Products' and 'Countries'
    'and output to columns A and B (respectively) of sheet 'wsOutput'.
    'Change to suit.
    
    Application.ScreenUpdating = False
    
    Set wsOutput = ThisWorkbook.Sheets("Sheet2")
    lngOutputRow = 2
    
    For Each rngCountry In ThisWorkbook.Names("Countries").RefersToRange
        For Each rngProduct In ThisWorkbook.Names("Products").RefersToRange
            With wsOutput
                .Range("A" & lngOutputRow).Value = rngCountry.Value
                .Range("B" & lngOutputRow).Value = rngProduct.Value
                lngOutputRow = lngOutputRow + 1
            End With
        Next rngProduct
    Next rngCountry
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Robert - Thank you very much for responding to my note. I have edited the code as below but it shows the error at Set OutputRange line. Could you please help me with this?

Sub LoopForCombinations()

Dim CountryRange, ProductRange As Range
Dim OutputRange As Range
Dim lngoutputrow As Long

Application.ScreenUpdating = False

Set CountryRange = ActiveSheet.Range("B9:B18")
Set ProductRange = ActiveSheet.Range("C22:C33")

Set OutputRange = ThisWorksheet.Range("K15")

For Each CountryRange In CountryRange.RefersToRange
For Each ProductRange In ProductRange.RefersToRange
With OutputRange
.Range("K" & lngoutputrow).Value = CountryRange.Value
.Range("L" & lngoutputrow).Value = ProductRange.Value
lngoutputrow = lngoutputrow + 1
End With
Next ProductRange
Next CountryRange

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Try this:

VBA Code:
Option Explicit
Sub LoopForCombinations()

    Dim CountryRange, ProductRange As Range
    Dim Country As Range, Product As Range
    Dim lngOutputrow As Long
    
    Application.ScreenUpdating = False
    
    Set CountryRange = Range("B9:B18")
    Set ProductRange = Range("C22:C33")
    lngOutputrow = 15
    
    For Each Country In CountryRange
        For Each Product In ProductRange
            Range("K" & lngOutputrow).Value = Country.Value
            Range("L" & lngOutputrow).Value = Product.Value
            lngOutputrow = lngOutputrow + 1
        Next Product
    Next Country
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Robert - It worked so perfectly. Thanks a ton. May I know how I can appreciate you for your help? Do we have some thumbsup button or recommendation, or some token of appreciation? Please advise. I am new to this. I thoroughly appreciate your time and support.
 
Upvote 0
Welcome to the MrExcel board!

Do we have some thumbsup button
Have a look at the bottom right of Robert's post(s)
1582673283264.png



BTW, you could also do this with formulas if you wanted. Each formula below copied down.

20 02 26.xlsm
ABCD
1ProductCountryProduct
2CPQBrazilCPQ
3SalesforceIQBrazilSalesforceIQ
4QuipBrazilQuip
5MC_Advertising_StudioBrazilMC_Advertising_Studio
6MC_Social_StudioBrazilMC_Social_Studio
7DataCloudBrazilDataCloud
8DataCloudBrazilDataCloud
9AnalyticsBrazilAnalytics
10Commerce_CloudBrazilCommerce_Cloud
11HerokuBrazilHeroku
12DMP_KruxBrazilDMP_Krux
13EssentialsBrazilEssentials
14SwedenCPQ
15SwedenSalesforceIQ
16CountriesSwedenQuip
17BrazilSwedenMC_Advertising_Studio
18SwedenSwedenMC_Social_Studio
19UKSwedenDataCloud
20IrelandSwedenDataCloud
21TurkeySwedenAnalytics
22IndiaSwedenCommerce_Cloud
23PhilippinesSwedenHeroku
24ChinaSwedenDMP_Krux
25ArgentinaSwedenEssentials
26RussiaUKCPQ
27UKSalesforceIQ
28UKQuip
List
Cell Formulas
RangeFormula
C2:C28C2=IF(ROWS(C$2:C2)>ROWS(A$2:A$13)*ROWS(A$17:A$26),"",INDEX(A$17:A$26,INT((ROWS(C$2:C2)-1)/ROWS(A$2:A$13))+1))
D2:D28D2=IF(C2="","",INDEX(A$2:A$13,MOD(ROWS(D$2:D2)-1,ROWS(A$2:A$13))+1))
 
Upvote 0
Hi *chaithanyakrishnagck,

You're welcome. Glad we got it sorted.

Hi Peter,

Thanks for the nifty formulas. The user now has a choice of both worlds.

Regards,

Robert
 
Upvote 0
Awesome, you both (Robert and Peter) are the best. I can't thank you both enough for such a quick response with an accurate solution. I am completely new to VBA and I started learning today. Your help and support means a lot to me.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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