+ Reply to Thread
Results 1 to 9 of 9

Macro with nested loop to combine each item of a colum with multiple items of another col

  1. #1
    Registered User
    Join Date
    09-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Macro with nested loop to combine each item of a colum with multiple items of another col

    Hi All,
    I have two columns with data as follows:

    Column A:
    Shoe white
    Shoe Black
    shoe Red

    Column B:
    Size 6
    Size 7
    Size 8
    Size 9
    Size 10

    I need to combine (concatenate) them as follows:


    Shoe white Size 6
    Shoe white Size 7
    Shoe white Size 8
    Shoe white Size 9
    Shoe white Size 10
    Shoe Black Size 6
    Shoe Black Size 7
    Shoe Black Size 8
    Shoe Black Size 9
    Shoe Black Size 10
    shoe Red Size 6
    shoe Red Size 7
    shoe Red Size 8
    shoe Red Size 9
    shoe Red Size 10

    I tried the following code:


    Sub proCombineUnevenColumnsVariables()

    Please Login or Register  to view this content.
    I have a couple of problems I would appreciate some help with:

    1.The code above gets a Run-time error:'13' Type Mismatch" error message. Besides the loop never ends, it executes even for empty cells (which is against the condition of the While loop) and I have to re-start excel to get out of it.
    2. The code above only solves one part of the problem (it merges 2 ranges) but I still need to work out how to nest it with another loop to concatenate each cell of one range with ALL CELLS of the other range.


    I would appreciate if someone could point me in the right direction.

    you all have a lovely day.

    Cheers,
    Juan

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi,

    You don't necessarily need a macro, and in any case should avoid looping macros if there is a standard Excel functionality you can use.

    Put

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    in C1 and copy it down 15 rows. Or generically for p x q rows where p & q are the number of items in each list.

    Then if necessary sort the 15 rows (after copying and pasting as values)

    If you're desperate to use a macro then you could of course encapsulate the above in a macro.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    09-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi Richard,
    Thanks a lot. It worked great and the exercise of deciphering is a real eye opener.
    Would you have a second to look at this attached image?
    Description of the requirement.jpg

    Your solution is great, I thought I needed a loop because (besides I didnt know the stuff you showed me) I also need to drag some columns related to column A and put them next to the concatenated result (the one your formula puts under C).
    so for example if:
    column A = Colour
    and next to it we had
    column B=price
    Column C =Availability
    Column D = size

    then the result I am after is:
    Column F= A+D (which your formula gives me)
    and next to each item on column F I will need the corresponding value on B, C and D.

    Would you have a second and point me in the right direction?
    Have good day

    cheers.
    Juan

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi,

    Sure. But would you upload the actual workbook (see forum guidance rules) rather than a picture clearly showing the result you expect.

  5. #5
    Registered User
    Join Date
    09-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Thumbs up Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi Richard,
    Thanks a lot for putting your time on this.
    I have uploaded the file, I hope it makes more sense to you now.

    also added a picture of it in case you can't read the call-outs I added to the spreadsheet.

    Have a good day.

    Kind regards,
    Juan

    Image of list of materials.jpg
    Attached Files Attached Files
    Last edited by Creare; 09-22-2013 at 12:12 AM. Reason: for clarity. added picture

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi,

    I'm still not clear what you're expecting

    Take Lysagt example. There are 5 unique items in column K and 3 unique thicknesses in column L meaning there are 15 permutations. Similarly with Stramit there are 2 unique in K and 2 unique in L giving 4 permutations and only 2 permutations for Stramit. Hence the sum of all permutations is 21.

    I'm not clear what the 496 current rows in column B mean and if this is the number of records you expect where they come from since their values are not present in K and L

  7. #7
    Registered User
    Join Date
    09-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi Richard,
    Sorry Im not making much sense here. I'll try again:

    The range F2:H39 has the categories and subcategories in which all the ITEMS come in. So for each item I will have 38 rows. For those 38 rows I will have:

    Column B: Is the item name which is simply =concatenate(K2,L2)... no combinations or permutation. Concatenate(K2,L2) repeats itself along 38 consecutive rows along column B.
    Column C: is the manufacturer wich in this case is M2. so M2 repeats itself down 38 consecutive rows on column C
    Columns D and E are to be ignored
    Columns F, G and H from row2 to row 39 (38 rows) get copied and placed next to the ITEM.

    Then we do the same for the next item:
    Column B: =Concatenate (K3,L3). which repeats from row 40 to row 77
    Column C = M3. repeated from row 40 to row 77
    Column F and G = os F2:H39 placed next to the the above on rows 40 to 77

    then we do the same with concatenate(K4:L4). we place them between rows 78 and 115. along column B
    M3 repeats itself 38 times between C78 and C115
    F2:H39 get copied over to F78:F115

    Hopefully this is clearer now. Please let me know if it isnt and apologies if Im wasting your time.

    Kind regards,
    Juan

  8. #8
    Registered User
    Join Date
    09-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi Richard,
    Was my response clearer? Please let me know
    Have a great day.

    Regards
    Juan

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi,

    Is the list in K2:M16 something you start with or has this somehow to be extracted from the existing data by the process you're trying to automate?

    Is the list in F2:H39 also something you start with and what determines that there are 38 rows. Is it simply the fact that column G starts repeating at row 40?

  10. #10
    Registered User
    Join Date
    09-11-2013
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Macro with nested loop to combine each item of a colum with multiple items of another

    Hi Richard,
    Both lists K2:M16 and F2:H39 are given. they are literally just typed in. F2:H39 are 38 rows because that is the amount of data I have.

    Cheers.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 05-31-2013, 11:45 AM
  2. Cross Reference item from one colum to items in a new column
    By mbrackey in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-07-2013, 03:36 PM
  3. Replies: 0
    Last Post: 11-13-2012, 12:53 PM
  4. For each loop nested; getting stuck on one cell in first iteration of nested loop
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-22-2009, 11:54 AM
  5. [SOLVED] Macro to combine (sum) like items
    By Scott Wagner in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-29-2005, 06:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1