Macro to extract column data into new rows

tparmbru22

New Member
Joined
Sep 7, 2017
Messages
15
I'm looking for help in creating a macro that can extract select column information and create new rows data but still keep the identifying information. Not all columns will have information and I only want to extract those that have data.

Below is an example of the survey data.

DateStoreNameTypeITStaffingFreightConstruction
1/1/2019123JoeNewComp Issue Missing Freight
2/1/2019234MikeReopenComp IssueShort Staff
3/1/2019345JohnNew Short Staff Delay Construction
4/1/2019456MaryExpansion Missing FreightDelay Construction

<colgroup><col span="3"><col><col><col><col><col></colgroup><tbody>
</tbody>

Below is how I'd like the macro output to look.

DateStoreNameTypeIssueDetails
1/1/2019123JoeNewITComp Issue
1/1/2019123JoeNewFreightMissing Freight
2/1/2019234MikeReopenITComp Issue
2/1/2019234MikeReopenStaffingShort Staff
3/1/2019345JohnNewStaffingShort Staff
3/1/2019345JohnNewConstructionDelay Construction
4/1/2019456MaryExpansionFreightMissing Freight
4/1/2019456MaryExpansionConstructionDelay Construction

<colgroup><col><col><col><col><col><col></colgroup><tbody>
</tbody>

Any help is greatly appreciated.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How about
Code:
Sub tparmbru22()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long, i As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary) * (UBound(Ary, 2) - 4), 1 To 6)
   For r = 2 To UBound(Ary)
      For c = 5 To UBound(Ary, 2)
         If Ary(r, c) <> "" Then
            nr = nr + 1
            For i = 1 To 4
               Nary(nr, i) = Ary(r, i)
            Next i
            Nary(nr, 5) = Ary(1, c)
            Nary(nr, 6) = Ary(r, c)
         End If
      Next c
   Next r
   Range("M2").Resize(nr, 6).Value = Nary
End Sub
 
Upvote 0
This is good but there are a few issues still.

In the example I only listed 4 columns of data issues but the actual survey can have upwards of 31 columns of data issues.

Also the macro kept the formatted data in the same worksheet. Is it possible to move it to a separate worksheet?


How about
Code:
Sub tparmbru22()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long, i As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary) * (UBound(Ary, 2) - 4), 1 To 6)
   For r = 2 To UBound(Ary)
      For c = 5 To UBound(Ary, 2)
         If Ary(r, c) <> "" Then
            nr = nr + 1
            For i = 1 To 4
               Nary(nr, i) = Ary(r, i)
            Next i
            Nary(nr, 5) = Ary(1, c)
            Nary(nr, 6) = Ary(r, c)
         End If
      Next c
   Next r
   Range("M2").Resize(nr, 6).Value = Nary
End Sub
 
Upvote 0
This will put the output on another sheet
Code:
Sub tparmbru22()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long, i As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary) * (UBound(Ary, 2) - 4), 1 To 6)
   For r = 2 To UBound(Ary)
      For c = 5 To UBound(Ary, 2)
         If Ary(r, c) <> "" Then
            nr = nr + 1
            For i = 1 To 4
               Nary(nr, i) = Ary(r, i)
            Next i
            Nary(nr, 5) = Ary(1, c)
            Nary(nr, 6) = Ary(r, c)
         End If
      Next c
   Next r
   Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("A2").Resize(nr, 6).Value = Nary
End Sub
change value in red to suit.
As long as you have a header in row1 for every column, the code should cater for however many columns there are.
 
Upvote 0
Thanks that worked perfectly!

This will put the output on another sheet
Code:
Sub tparmbru22()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long, i As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary) * (UBound(Ary, 2) - 4), 1 To 6)
   For r = 2 To UBound(Ary)
      For c = 5 To UBound(Ary, 2)
         If Ary(r, c) <> "" Then
            nr = nr + 1
            For i = 1 To 4
               Nary(nr, i) = Ary(r, i)
            Next i
            Nary(nr, 5) = Ary(1, c)
            Nary(nr, 6) = Ary(r, c)
         End If
      Next c
   Next r
   Sheets("[COLOR=#ff0000]Sheet1[/COLOR]").Range("A2").Resize(nr, 6).Value = Nary
End Sub
change value in red to suit.
As long as you have a header in row1 for every column, the code should cater for however many columns there are.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,765
Members
449,049
Latest member
greyangel23

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