Loop through multiple columns and transposing data

mozza90

New Member
Joined
Mar 19, 2015
Messages
32
Hi there,

I have some data which is in the below format. I'd like to manipulate this so it looks like the second table. However I'm having some trouble with my loop where I can only run it for client 1, and can't figure out how to add client 2 and 3 to the column K with the respective quantities, price and product.

ABCDEF
ProductQuantityPriceClient 1Client 2Client 3
Bread10$1.00433
Milk12$0.80804

<tbody>
</tbody>

HIJK
ProductQuantityPriceClient
Bread41.00Client 1
Bread31.00Client 2
Bread31.00Client 3
Milk80.80Client 1
Milk40.80Client 3

<tbody>
</tbody>


Code:
 Dim row As Integer


     row = 2

Do While .Range("A" & row) <> ""
            If .Range("B" & row) > 0 Then
               .Range("H" & row) = Range("A" & row)
               .Range("I" & row) = Range("B" & row)
               .Range("J" & row) = Range("C" & row)
               .Range("D" & row) = Range("K" & row)


'doesn't continue to add these clients

               .Range("E" & row) = Range("K" & row)
               .Range("F" & row) = Range("K" & row)
             
'etc....




              End If
                 
                 
                 row = row + 1
         Loop


Any ideas of where I'm going wrong?

Ta!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
How about
Code:
Sub TransposeData()
   Dim ary As Variant, Nary() As Variant
   Dim r As Long, c As Long, j As Long
   ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(ary) * 3, 1 To 4)
   j = j + 1
   For c = 1 To 3
      Nary(1, c) = ary(1, c)
   Next c
   Nary(1, 4) = "Client"
   For r = 2 To UBound(ary)
      For c = 4 To UBound(ary, 2)
         If ary(r, c) <> 0 Then
            j = j + 1
            Nary(j, 1) = ary(r, 1)
            Nary(j, 2) = ary(r, c)
            Nary(j, 3) = ary(r, 3)
            Nary(j, 4) = ary(1, c)
         End If
      Next c
   Next r
   Range("H1").Resize(j, 4).Value = Nary
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,810
Messages
6,121,690
Members
449,048
Latest member
81jamesacct

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