Change Data Layout

DanniiMarie

New Member
Joined
May 21, 2018
Messages
30
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
I have a manifest of items shipped to store locations but it needs to be uploaded to a service in a very specific format. I know there is a quick way to complete this task using a macro but just don't know where to start. Here is the format I have:

Store #
Store Name
Item #1Item #2Item#3Item #4
1234Wowza on Main1004
5678Wowza on 2nd5030
9012Wowza on 15th1001
3456Wowza on 22nd1211

The format I need ignores the zeros and rearranges things like this:

Store NameContentStore #
Wowza on MainItem 11234
Wowza on MainItem 41234
Wowza on 2ndItem 15678
Wowza on 2ndItem 35678
Wowza on 15thItem 19012
Wowza on 15thItem 49012
Wowza on 22ndItem 13456
Wowza on 22ndItem 23456
Wowza on 22ndItem 33456
Wowza on 22ndItem 43456

Notice how it doesn't matter what the # is in the item columns so long as it is not a zero. The new table can be on a new tab and the tab title doesn't matter as I can simply cut and paste the results of the macro into my upload sheet.

Can someone help me with this?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Give this a try with a copy of your data.

VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
  
  a = Range("A1").CurrentRegion.Value
  uba2 = UBound(a, 2)
  ReDim b(1 To UBound(a) * (uba2 - 2), 1 To 3)
  For i = 2 To UBound(a)
     For j = 3 To uba2
      If a(i, j) <> 0 Then
        k = k + 1
        b(k, 1) = a(i, 2)
        b(k, 2) = a(1, j)
        b(k, 3) = a(i, 1)
      End If
     Next j
  Next i
  Range("A" & Rows.Count).End(xlUp).Offset(3).Resize(k, UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Solution
Give this a try with a copy of your data.

VBA Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, uba2 As Long
 
  a = Range("A1").CurrentRegion.Value
  uba2 = UBound(a, 2)
  ReDim b(1 To UBound(a) * (uba2 - 2), 1 To 3)
  For i = 2 To UBound(a)
     For j = 3 To uba2
      If a(i, j) <> 0 Then
        k = k + 1
        b(k, 1) = a(i, 2)
        b(k, 2) = a(1, j)
        b(k, 3) = a(i, 1)
      End If
     Next j
  Next i
  Range("A" & Rows.Count).End(xlUp).Offset(3).Resize(k, UBound(b, 2)).Value = b
End Sub

That was the help I needed! THANK YOU! You saved a lot of people A LOT of unnecessary keying!
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

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