Excel VBA to Create and Autofill New Table

Slazar

New Member
Joined
Aug 28, 2015
Messages
17
Hi all,

I'm mediocre at best with VBA so need quite a lot of help and may be asking questions to understand better.
I need a macro that does the following:

User will input the information in the table below. User will input a category (Fruit) and an identifying label (Color).
User will then click a button tied to the macro. The macro will return the filled out table in a newly created sheet.
For the table created by the macro, the cells will need to have all borders filled.

Information on the outputted table will be based on another table (containing ingredients) in another sheet.


Input from User:
AB
1FruitColor (ID)
2AppleGreen
3AppleRed

<tbody>
</tbody>

User then Clicks Button

Hidden Table Already Filled Out in Another Sheet:
AB
1FruitIngredient
2
AppleFlour
3AppleSugar
4AppleCinnamon

<tbody>
</tbody>

Output from Macro in Newly Created Sheet:
ABCD
1Item #
FruitColorIngredient
21AppleGreenFlour
32AppleGreenSugar
43AppleGreenCinnamon
54AppleRedFlour
65AppleRedSugar
76AppleRedCinnamon

<tbody>
</tbody>
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
"User" Table on sheet1
"Hidden Table" on sheet2
Results on Sheet.

Code:
[COLOR="Navy"]Sub[/COLOR] MG25Jul49
[COLOR="Navy"]Dim[/COLOR] Rng1 [COLOR="Navy"]As[/COLOR] Range, Dn1 [COLOR="Navy"]As[/COLOR] Range, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Rng2 [COLOR="Navy"]As[/COLOR] Range, Dn2 [COLOR="Navy"]As[/COLOR] Range
c = 1
[COLOR="Navy"]With[/COLOR] Sheets("Sheet1")
    [COLOR="Navy"]Set[/COLOR] Rng1 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Sheets("Sheet2")
    [COLOR="Navy"]Set[/COLOR] Rng2 = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
ReDim ray(1 To Rng1.Count * Rng2.Count + 1, 1 To 4)
ray(1, 1) = "Item #": ray(1, 2) = "Fruit": ray(1, 3) = "Color": ray(1, 4) = "Ingredient"

[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn1 [COLOR="Navy"]In[/COLOR] Rng1
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn2 [COLOR="Navy"]In[/COLOR] Rng2
            [COLOR="Navy"]If[/COLOR] Dn2.Value = Dn1.Value [COLOR="Navy"]Then[/COLOR]
              c = c + 1
           ray(c, 1) = c - 1: ray(c, 2) = Dn1.Value: ray(c, 3) = Dn1.Offset(, 1).Value
            ray(c, 4) = Dn2.Offset(, 1).Value
               
            [COLOR="Navy"]End[/COLOR] If
        
        [COLOR="Navy"]Next[/COLOR] Dn2
[COLOR="Navy"]Next[/COLOR] Dn1
[COLOR="Navy"]With[/COLOR] Sheets("Sheet3").Range("A1").Resize(c, 4)
    .Value = ray
    .Borders.Weight = 2
    .Columns.AutoFit
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,215,030
Messages
6,122,762
Members
449,095
Latest member
m_smith_solihull

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