Create a new table starting from another one

anpak

New Member
Joined
Oct 19, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm new on this forum. Below my first question :)

I would like to create a macro in order to obtain the table named "Output" starting from the table named "Input". Please see the image below.

Is it possible?

1634652188479.png



Thank you in advance :)
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi & welcome to MrExcel.
Any reason you want a macro, as this can be done with a formula?
 
Upvote 0
Hello fluff,

how can I do that with formulas?

I asked for a macro because the table "input" could have different size (it becomes from an external report).

Thank you
 
Upvote 0
Ok, will it always be 5 columns wide with a varying number of rows?
 
Upvote 0
The table "Input" can have maximum 50 colums and variable rows
 
Upvote 0
Is it an actual table, or just a range of cells?
Also does it have a header row?
 
Upvote 0
No it is not formatted as table, it is just a range of cells
 
Upvote 0
ops sorry, the header row is present, but we don't have to consider it.
 
Upvote 0
Ok, how about
VBA Code:
Sub anpak()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   With Sheets("Sheet1")
      c = .Cells(1, Columns.Count).End(xlToLeft).Column
      Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Resize(, c).Value2
   End With
   ReDim Nary(1 To UBound(Ary) * c, 1 To 2)
   
   For r = 1 To UBound(Ary)
      For c = 2 To UBound(Ary, 2)
         If Ary(r, c) <> "" Then
            nr = nr + 1
            Nary(nr, 1) = Ary(r, 1)
            Nary(nr, 2) = Ary(r, c)
         End If
      Next c
   Next r
   Sheets("Sheet2").Range("A2").Resize(nr, 2).Value = Nary
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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