VBA Transpose data from one sheet into another

madi1004

New Member
Joined
Dec 11, 2013
Messages
11
Hello,

I need your support in creating a vba and transposing data into another sheet

Original sheet from where the data is needed to be copied

VBA Transpose.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAY
1rate zone coderate zone nameorigin addressdestination addresscurrency1 Pal2 Pal3 Pal4 Pal5 Pal6 Pal7 Pal8 Pal9 Pal10 Pal11 Pal12 Pal13 Pal14 Pal15 Pal16 Pal17 Pal18 Pal19 Pal20 Pal21 Pal22 Pal23 Pal24 Pal25 Pal26 Pal27 Pal28 Pal29 Pal30 Pal31 Pal32 Pal33 Pal34 Pal35 Pal36 Pal37 Pal38 Pal39 Pal40 Pal45 Pal50 Pal55 Pal60 PalLead time door to door [hours] Comments
2ratezone code 1ratezone name 1origin address 1destination address 1USD60110160210260310360410460510560610660710760810860910960101010601110116012101260131013601410146015101560161016601710176018101860191019602010206021102160221024 hcomment 1
3ratezone code 2ratezone name 2origin address 2destination address 2USD80130180230280330380430480530580630680730780830880930980103010801130118012301280133013801430148015301580163016801730178018301880193019802030208021302180223048 hcomment 2
4ratezone code 3ratezone name 3origin address 3destination address 3USD90140190240290340390440490540590640690740790840890940990104010901140119012401290134013901440149015401590164016901740179018401890194019902040209021402190224072 hcomment 3
5ratezone code 4ratezone name 4origin address 4destination address 4USD1001502002503003504004505005506006507007508008509009501000105011001150120012501300135014001450150015501600165017001750180018501900195020002050210021502200225093 hcomment 4
6ratezone code 5ratezone name 5origin address 5destination address 5USD15020025030035040045050055060065070075080085090095010001050110011501200125013001350140014501500155016001650170017501800185019001950200020502100215022002250230048 hcomment 5
LTL
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2,A4,A6Expression=LEN(A2)>32textNO
B2:B6Expression=LEN(B2)>64textNO


And I would need your help to create a VBA code to transpose the data and at the end to look like this

VBA Transpose.xlsx
ABCD
1rate zone codefrom palletto palletrate
2ratezone code 11160
3ratezone code 122110
4ratezone code 133160
5ratezone code 144210
6ratezone code 155260
7ratezone code 166310
8ratezone code 177360
9ratezone code 188410
10ratezone code 199460
11ratezone code 11010510
12ratezone code 11111560
13ratezone code 11212610
14ratezone code 11313660
15ratezone code 11414710
16ratezone code 11515760
17ratezone code 11616810
18ratezone code 11717860
19ratezone code 11818910
20ratezone code 11919960
21ratezone code 120201010
22ratezone code 121211060
23ratezone code 122221110
24ratezone code 123231160
25ratezone code 124241210
26ratezone code 125251260
27ratezone code 126261310
28ratezone code 127271360
29ratezone code 128281410
30ratezone code 129291460
31ratezone code 130301510
32ratezone code 131311560
33ratezone code 132321610
34ratezone code 133331660
35ratezone code 134341710
36ratezone code 135351760
37ratezone code 136361810
38ratezone code 137371860
39ratezone code 138381910
40ratezone code 139391960
41ratezone code 140402010
42ratezone code 145452060
43ratezone code 150502110
44ratezone code 155552160
45ratezone code 160602210
46ratezone code 21180
47ratezone code 222130
48ratezone code 233180
49ratezone code 244230
50ratezone code 255280
51ratezone code 266330
52ratezone code 277380
53ratezone code 288430
54ratezone code 299480
55ratezone code 21010530
56ratezone code 21111580
57ratezone code 21212630
58ratezone code 21313680
59ratezone code 21414730
60ratezone code 21515780
61ratezone code 21616830
62ratezone code 21717880
63ratezone code 21818930
64ratezone code 21919980
65ratezone code 220201030
66ratezone code 221211080
67ratezone code 222221130
68ratezone code 223231180
69ratezone code 224241230
70ratezone code 225251280
71ratezone code 226261330
72ratezone code 227271380
73ratezone code 228281430
74ratezone code 229291480
75ratezone code 230301530
76ratezone code 231311580
77ratezone code 232321630
78ratezone code 233331680
79ratezone code 234341730
80ratezone code 235351780
81ratezone code 236361830
82ratezone code 237371880
83ratezone code 238381930
84ratezone code 239391980
85ratezone code 240402030
86ratezone code 245452080
87ratezone code 250502130
88ratezone code 255552180
89ratezone code 260602230
90ratezone code 31190
91ratezone code 322140
92ratezone code 333190
93ratezone code 344240
94ratezone code 355290
95ratezone code 366340
96ratezone code 377390
97ratezone code 388440
98ratezone code 399490
99ratezone code 31010540
100ratezone code 31111590
101ratezone code 31212640
102ratezone code 31313690
103ratezone code 31414740
104ratezone code 31515790
105ratezone code 31616840
106ratezone code 31717890
107ratezone code 31818940
108ratezone code 31919990
109ratezone code 320201040
110ratezone code 321211090
111ratezone code 322221140
112ratezone code 323231190
113ratezone code 324241240
114ratezone code 325251290
115ratezone code 326261340
116ratezone code 327271390
117ratezone code 328281440
118ratezone code 329291490
119ratezone code 330301540
120ratezone code 331311590
121ratezone code 332321640
122ratezone code 333331690
123ratezone code 334341740
124ratezone code 335351790
125ratezone code 336361840
126ratezone code 337371890
127ratezone code 338381940
128ratezone code 339391990
129ratezone code 340402040
130ratezone code 345452090
131ratezone code 350502140
132ratezone code 355552190
133ratezone code 360602240
134ratezone code 411100
135ratezone code 422150
136ratezone code 433200
137ratezone code 444250
138ratezone code 455300
139ratezone code 466350
140ratezone code 477400
141ratezone code 488450
142ratezone code 499500
143ratezone code 41010550
144ratezone code 41111600
145ratezone code 41212650
146ratezone code 41313700
147ratezone code 41414750
148ratezone code 41515800
149ratezone code 41616850
150ratezone code 41717900
151ratezone code 41818950
152ratezone code 419191000
153ratezone code 420201050
154ratezone code 421211100
155ratezone code 422221150
156ratezone code 423231200
157ratezone code 424241250
158ratezone code 425251300
159ratezone code 426261350
160ratezone code 427271400
161ratezone code 428281450
162ratezone code 429291500
163ratezone code 430301550
164ratezone code 431311600
165ratezone code 432321650
166ratezone code 433331700
167ratezone code 434341750
168ratezone code 435351800
169ratezone code 436361850
170ratezone code 437371900
171ratezone code 438381950
172ratezone code 439392000
173ratezone code 440402050
174ratezone code 445452100
175ratezone code 450502150
176ratezone code 455552200
177ratezone code 460602250
178ratezone code 511150
179ratezone code 522200
180ratezone code 533250
181ratezone code 544300
182ratezone code 555350
183ratezone code 566400
184ratezone code 577450
185ratezone code 588500
186ratezone code 599550
187ratezone code 51010600
188ratezone code 51111650
189ratezone code 51212700
190ratezone code 51313750
191ratezone code 51414800
192ratezone code 51515850
193ratezone code 51616900
194ratezone code 51717950
195ratezone code 518181000
196ratezone code 519191050
197ratezone code 520201100
198ratezone code 521211150
199ratezone code 522221200
200ratezone code 523231250
201ratezone code 524241300
202ratezone code 525251350
203ratezone code 526261400
204ratezone code 527271450
205ratezone code 528281500
206ratezone code 529291550
207ratezone code 530301600
208ratezone code 531311650
209ratezone code 532321700
210ratezone code 533331750
211ratezone code 534341800
212ratezone code 535351850
213ratezone code 536361900
214ratezone code 537371950
215ratezone code 538382000
216ratezone code 539392050
217ratezone code 540402100
218ratezone code 545452150
219ratezone code 550502200
220ratezone code 555552250
221ratezone code 560602300
Transpose VBA
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:A221Expression=LEN(A2)>32textNO


Thank you
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try this

VBA Code:
Sub TransposeAndFormatData()
    Dim ws As Worksheet
    Dim sourceRange As Range, destRange As Range
    Dim lastRow As Long, lastCol As Long
    Dim i As Long, j As Long, rowIndex As Long
    
    Set ws = ThisWorkbook.Sheets("Sheet10") ' Replace "Sheet1" with the actual sheet name
    
    ' last rows/cols
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    
    Set sourceRange = ws.Range(ws.Cells(2, 6), ws.Cells(lastRow, lastCol))
    
    Set destRange = ws.Cells(14, 1) ' might put this on a seperate sheet, just below my sample data now
    
    For i = 1 To sourceRange.Rows.Count
        For j = 1 To sourceRange.Columns.Count
            rowIndex = (i - 1) * sourceRange.Columns.Count + j
            
            ' write out
            destRange.Offset(rowIndex - 1, 0).Value = ws.Cells(i + 1, 1).Value
            destRange.Offset(rowIndex - 1, 1).Value = j
            destRange.Offset(rowIndex - 1, 2).Value = j
            destRange.Offset(rowIndex - 1, 3).Value = sourceRange.Cells(i, j).Value
        Next
    Next
    
    'headers
    ws.Cells(14 - 1, 1).Value = "rate zone code"
    ws.Cells(14 - 1, 2).Value = "from pallet"
    ws.Cells(14 - 1, 3).Value = "to pallet"
    ws.Cells(14 - 1, 4).Value = "rate"
End Sub
 
Upvote 1
I have found something, but still need your support:

VBA Code:
Sub Transpose()

  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  Dim bln As Boolean
  
  a = Sheets("LTL").Range("A1").CurrentRegion
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 2)
  
  k = 1
  For i = 2 To UBound(a, 1)
    bln = False
    For j = 6 To UBound(a, 2)
      b(k, 1) = a(i, 1)
      If a(i, j) <> "" Then
        b(k, 2) = a(i, j)
        k = k + 1
        bln = True
      End If
    Next
    If bln = False Then k = k + 1
  Next
  Sheets("Transposed").Range("A2").Resize(k, 2).Value = b
End Sub

It takes in consideration also the last 2 columns, from the first sheet, which I don't need .

It does not transpose the columns with the pallets (1-60).
 
Upvote 0
Did you try the code in post #4 above?
yes, I tried it now thank you.

I did some changes, for the last 2 columns as they are not needed , and also as you mentioned regarding the second sheet
VBA Code:
ub Transposedata()

    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Dim sourceRange As Range, destRange As Range
    Dim lastRow As Long, lastCol As Long
    Dim i As Long, j As Long, rowIndex As Long
    
    Set ws = ThisWorkbook.Sheets("LTL") ' Replace "Sheet1" with the actual sheet name
    Set ws2 = ThisWorkbook.Sheets("Transposed")
    ' last rows/cols
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column - 2
    
    Set sourceRange = ws.Range(ws.Cells(2, 6), ws.Cells(lastRow, lastCol))
    
    Set destRange = ws2.Cells(2, 1) ' might put this on a seperate sheet, just below my sample data now
    
    For i = 1 To sourceRange.Rows.Count
        For j = 1 To sourceRange.Columns.Count
            rowIndex = (i - 1) * sourceRange.Columns.Count + j
            
            ' write out
            destRange.Offset(rowIndex - 1, 0).Value = ws.Cells(i + 1, 1).Value
            destRange.Offset(rowIndex - 1, 1).Value = j
            destRange.Offset(rowIndex - 1, 2).Value = j
            destRange.Offset(rowIndex - 1, 3).Value = sourceRange.Cells(i, j).Value
        Next
    Next
    
    'headers
    ws2.Cells(1, 1).Value = "rate zone code"
    ws2.Cells(1, 2).Value = "from pallet"
    ws2.Cells(1, 3).Value = "to pallet"
    ws2.Cells(1, 4).Value = "rate"
End Sub

The issue that I have now, is regarding the last pallets, as now it is considering 40-44, but my last pallets are 40, 45, 50 , 55 , 60
Can you please take a look also on this.
Maybe there is a possibility to copy / transpose from F1 to AW1 - as the format of the cell is a number.

Regards,
Dieter
 
Upvote 0
I'm out for the rest of the day. Bump this if it's not solved by tomorrow and I'll look at it.

I would probably change the approach to an array if you have a ton of rate zone codes.
 
Upvote 0
Hi, give this code a try on a copy of your workbook

VBA Code:
Sub Pallets()
Dim sh1 As Worksheet, sht As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  Dim lr As Long
 
Sheets.Add.Name = "Temp"  'adds a sheet named Temp
  Set sh1 = Sheets("LTL") ' Change this name to the name of your sheet.
  Set sht = Sheets("Temp")
 
  lr = sh1.Range("A" & Rows.Count).End(xlUp).Row

  a = sh1.Range("A1:AW" & lr).Value
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 4)
 
  For i = 2 To UBound(a, 1)
        For j = 6 To UBound(a, 2)
        k = k + 1

            b(k, 1) = a(i, 1)
            b(k, 2) = a(1, j)
            b(k, 3) = a(1, j)
            b(k, 4) = a(i, j)
   
    Next
   Next
   sht.Range("A2").Resize(k, 4).Value = b
 Range("A1:D1").Value = Array("Rate Zone Code", "From Pallet", "To Pallet", "Rate")
End Sub
 
Upvote 1
Solution
Hi, give this code a try on a copy of your workbook

VBA Code:
Sub Pallets()
Dim sh1 As Worksheet, sht As Worksheet
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  Dim lr As Long
 
Sheets.Add.Name = "Temp"  'adds a sheet named Temp
  Set sh1 = Sheets("LTL") ' Change this name to the name of your sheet.
  Set sht = Sheets("Temp")
 
  lr = sh1.Range("A" & Rows.Count).End(xlUp).Row

  a = sh1.Range("A1:AW" & lr).Value
  ReDim b(1 To UBound(a, 1) * UBound(a, 2), 1 To 4)
 
  For i = 2 To UBound(a, 1)
        For j = 6 To UBound(a, 2)
        k = k + 1

            b(k, 1) = a(i, 1)
            b(k, 2) = a(1, j)
            b(k, 3) = a(1, j)
            b(k, 4) = a(i, j)
  
    Next
   Next
   sht.Range("A2").Resize(k, 4).Value = b
 Range("A1:D1").Value = Array("Rate Zone Code", "From Pallet", "To Pallet", "Rate")
End Sub
Thank you for your support.
It works now
 
Upvote 0

Forum statistics

Threads
1,223,872
Messages
6,175,104
Members
452,613
Latest member
amorehouse

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