Create a new row based on multiple columns

caudle

New Member
Joined
Oct 24, 2016
Messages
2
Not sure if the title correctly identifies this but here is what I am looking to do. I have a very large dataset but need it in a different format.

I need to transform this:
name
app1
app2
app3
computer 1
1
2
3

<tbody>
</tbody>


To this:
Name
App
computer 1
1
computer 1
2
computer 1
3

<tbody>
</tbody>
I have found VBA that takes the column and creates a new row but not one that copies the first column/row information.

I appreciate any help you can provide.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
caudle,

Welcome to the MrExcel forum.

Here is a macro solution for you to consider that will adjust to the number of raw data rows, and, columns, and, that uses two arrays in memory, and, should be fast.

You can change the raw data worksheet name in the macro.

Sample raw data:


Excel 2007
ABCDE
1nameapp1app2app3
2computer 1123
3
4
5
Sheet1


And, after the macro:


Excel 2007
ABCDE
1NameApp
2computer 11
3computer 12
4computer 13
5
Sheet1


And, if we change the raw data to this:


Excel 2007
ABCDEF
1nameapp1app2app3app4
2computer 11234
3computer 2567
4
5
6
7
8
9
Sheet1


And, run the macro again, we get this:


Excel 2007
ABCDEF
1NameApp
2computer 11
3computer 12
4computer 13
5computer 14
6computer 25
7computer 26
8computer 27
9
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub TransformData()
' hiker95, 10/25/2016, ME972307
Dim a As Variant, i As Long, c As Long
Dim o As Variant, j As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")   '<-- you can change the sheet name here
  a = .Cells(1).CurrentRegion
  ReDim o(1 To UBound(a, 1) * (UBound(a, 2) - 2), 1 To 2)
  j = j + 1: o(j, 1) = "Name": o(j, 2) = "App"
  For i = 2 To UBound(a, 1)
    For c = 2 To UBound(a, 2)
      If Not a(i, c) = vbEmpty Then
        j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(i, c)
      End If
    Next c
  Next i
  .Cells(1).CurrentRegion.ClearContents
  .Cells(1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(1).Resize(, UBound(o, 2)).AutoFit
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the TransformData macro.
 
Upvote 0
Thank you very much hiker95!! That worked marvelously! I am new to heavy data processing with excel and VBA so I am learning the ropes. This helped me out a lot!
 
Upvote 0
caudle,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,948
Messages
6,127,871
Members
449,410
Latest member
adunn_23

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