How to create multiple rows combining data sets

1

I have three tables and i need an out put which has data from two tables, i would like to create the same using macro.

Table A

A
B
C
D

Table B

Apple
Orange
Pears

Table C

Americas
Asia
Europe

The Output i need is

A Apple Americas
A Apple Asia
A Apple Europe
A Orange Americas
A Orange Asia
A Orange Europe

So i guess for each letter from Table A, there would be 12 additional rows created. The above is the sample data and I have around 5000 rows in Table A.

user3197779

Posted 2014-01-15T11:54:17.700

Reputation: 13

Hi Raystafarian, i generally use MS Access and create three different tables and then just create a query which would provide the required output. However, we no more have access to MS Access. Hence struggling to find a way out. – user3197779 – 2014-01-15T12:11:41.143

I have another question, in case in Table C i have data as mentioned below, with two columns Table C Americas USD Asia JPY Europe EUR

And the Output would be something like this A Apple Americas USD A Apple Asia JYP A Apple Europe EUR A Orange Americas USD A Orange Asia JPY A Orange Europe EUR

How to go about in this case. Sorry, I guess should have pointed out in the first instance itself. – user3197779 – 2014-01-15T12:53:55.990

just offset the write location and the read location. So you could do cells(i,3) = e.value AND cells(i,4) = e.offset(0,1).value – Raystafarian – 2014-01-15T13:54:17.397

Hi Ray, Again with an issue, this time, the query runs but creates only till 32,727 rows, but i am expecting that my data would have around 150,000 rows. I error i get is run time error 6

Based on some web research i made "Dim i As Long" and run the query but no help. – user3197779 – 2014-01-15T18:19:48.600

What version excel are you using? And what line gets highlighted with the overflow? – Raystafarian – 2014-01-15T19:00:15.753

Hi Ray, I again ran the query today morning making the "Dim I As Long" and it worked fine. Thanks.

Also, i am currently trying to build a macro and i might end up requesting help from you..so if you can share me ur email id, i can reach out to you in future. If only if you are willing to though:) – user3197779 – 2014-01-16T07:40:33.670

Answers

1

This will do it, just modify as appropriate

Sub Umesh()
Application.ScreenUpdating = False
Dim i As Integer
Dim c As Range
Dim d As Range
Dim e As Range

i = 1

For Each c In Worksheets("Sheet1").Range("A:A")
If c <> "" Then

    For Each d In Worksheets("Sheet2").Range("A:A")
    If d <> "" Then

        For Each e In Worksheets("sheet3").Range("A:A")
            If e <> "" Then

            Worksheets("sheet4").Cells(i, 1) = c.Value
            Worksheets("sheet4").Cells(i, 2) = d.Value
            Worksheets("Sheet4").Cells(i, 3) = e.Value
            i = i + 1

            End If
        Next e

    End If
    Next d

End If
Next c

Application.ScreenUpdating = True
End Sub

Raystafarian

Posted 2014-01-15T11:54:17.700

Reputation: 20 384

1Thank you Raystafarian. The code is working fine. Very helpful. – user3197779 – 2014-01-15T12:46:08.570