VBA Macro Transpose/Delete Rows and continue down spreadsheet with differing total number of rows

krishna008

New Member
Joined
Jan 7, 2010
Messages
12
Hi All,

I have a little bit of VBA knowledge but am struggling with this one.

I have a data extraction from an accounting system. The first half of the data is represented horizontally as expected, but the second half of the data is somehow expressed vertically (a can of worms I don't need to go into right now).

I need a way to transpose this vertical data to the corresponding row, delete the rows no longer required, then continue for the next record and so on. This data will having varying rows so ideally need to make sure it can handle that.

Here is a screenshot of the spreadsheet to show you what I mean but I can attach a truncated copy of the file if needed.
First I need just one version of the data in AB2:AB55 to copies and transposed to AD1 to provide headings.
Then need AC2:AC55 to be transposed to AD2. I then need rows 3:55 to be deleted.
Then need to repeat the same for the next WOnumber (which is in column A) but it will be on the next row down (AC3:56 transposed to AD3, then delete 4:56 to be deleted and so on).

I go the vb macro code from the mr.excel forum, however my sheet has the empty values in the AC column. and also I have the different data types. I want to copy what ever there under AC column and transpose them under AD.

I will be adding some formatting to the code but I can add this myself :)

Thank you in advance!

Data sheet. unable to post entire sheet due to restriction of 300 cells, but I will have the 100+ WO# and corresponding data in the other cells. Here I gave only one WO# example.

Input.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1WO#Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26QuestionAnswer
21234Row1.01Row1.02Row1.03Row1.04Row1.05Row1.06Row1.07Row1.08Row1.09Row1.10Row1.11Row1.12Row1.13Row1.14Row1.15Row1.16Row1.17Row1.18Row1.19Row1.20Row1.21Row1.22Row1.23Row1.24Row1.25Row1.26Question1Answer1
3Question2Answer2
4Question3Answer3
5Question4Answer4
6Question5Answer5
7Question6Answer6
8Question7Answer7
9Question8Answer8
10Question9Answer9
11Question10Answer10
12Question11Answer11
13Question12Answer12
14Question13Answer13
15Question14Answer14
16Question15Answer15
17Question16Answer16
18Question17Answer17
19Question18Answer18
20Question19Answer19
21Question20Answer20
22Question21Answer21
23Question22Answer22
24Question23Answer23
25Question24Answer24
26Question25Answer25
27Question26
28Question27Answer27
29Question28Answer28
30Question29Answer29
31Question30Answer30
32Question31Answer31
33Question32Answer32
34Question33Answer33
35Question34Answer34
36Question35Answer35
37Question36Answer36
38Question37Answer37
39Question38Answer38
40Question39Answer39
41Question40Answer40
42Question41
43Question42Answer42
44Question43Answer43
45Question44
46Question45Answer45
47Question46Answer46
48Question47Answer47
49Question48Answer48
50Question49Answer49
51Question50Answer50
52Question51Answer51
53Question52Answer52
54Question53Answer53
55Question54Answer54
Data



Expected Reusult:
Macro.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACBCCCDCE
1WO#Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26QuestionAnswerQuestion1Question2Question3Question4Question5Question6Question7Question8Question9Question10Question11Question12Question13Question14Question15Question16Question17Question18Question19Question20Question21Question22Question23Question24Question25Question26Question27Question28Question29Question30Question31Question32Question33Question34Question35Question36Question37Question38Question39Question40Question41Question42Question43Question44Question45Question46Question47Question48Question49Question50Question51Question52Question53Question54
21234Row1.01Row1.02Row1.03Row1.04Row1.05Row1.06Row1.07Row1.08Row1.09Row1.10Row1.11Row1.12Row1.13Row1.14Row1.15Row1.16Row1.17Row1.18Row1.19Row1.20Row1.21Row1.22Row1.23Row1.24Row1.25Row1.26Question1Answer1Answer1Answer2Answer3Answer4Answer5Answer6Answer7Answer8Answer9Answer10Answer11Answer12Answer13Answer14Answer15Answer16Answer17Answer18Answer19Answer20Answer21Answer22Answer23Answer24Answer25Answer27Answer28Answer29Answer30Answer31Answer32Answer33Answer34Answer35Answer36Answer37Answer38Answer39Answer40Answer42Answer43Answer45Answer46Answer47Answer48Answer49Answer50Answer51Answer52Answer53Answer54
Sheet1


I will delete the original AB:AC columns using the "columns("AB:AC").Delete" option.

This is the code I got it from the Mr.Excel forum, but it does not work if the AC column has many empty values.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I think the following does what you asked for:

VBA Code:
Sub TransposeAndPaste()
'
    Dim LoopCount                   As Long, NumberOfWOs                    As Long
    Dim DataSheetColumn_AB_Array    As Variant, DataSheetColumn_AC_Array    As Variant
'
    Application.ScreenUpdating = False
'
    With Sheets("Data")
        DataSheetColumn_AB_Array = Application.Transpose(.Range("AB2:AB55").Value2)                                 '   Save "Data" sheet column AB values from rows 2:55 into DataSheetColumn_AB_Array
'
        .Range("AD1").Resize(1, UBound(DataSheetColumn_AB_Array, 1)) = DataSheetColumn_AB_Array                     '   Paste the transposed data from Column AB to the horizontal range
'
        NumberOfWOs = .Range("A" & Rows.Count).End(xlUp).Row - 1                                                    '   Get the # of work orders from Column A in 'Data' sheet
'
        For LoopCount = 1 To NumberOfWOs                                                                            '   Loop through each work order in Column A
            DataSheetColumn_AC_Array = Application.Transpose(.Range("AC2:AC55").Value2)                             '       Save "Data" sheet column AC values from rows 2:55 into DataSheetColumn_AC_Array
            .Range("AD" & LoopCount + 1).Resize(1, UBound(DataSheetColumn_AC_Array, 1)) = DataSheetColumn_AC_Array  '       Paste the transposed data from Column AC to the horizontal range
'
            .Range("AC2:AC55").Delete Shift:=xlUp                                                                   '       Delete Previous transposed data range since it is no longer needed
        Next                                                                                                        '   Loop back
'
        .Columns("AB:AC").Delete                                                                                    '   Delete the Columns that were transposed
    End With
'
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for your quick reply. I tried the macro, it does transpose the first and second rows of the question data, but failed to bring the rows from A:AA from the second work order onwards. the second row values are still in their original row. we want to move them back to top to aglign with the transposed data.
here is the result:
Macro.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACBCC
1WO#Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26Question1Question2Question3Question4Question5Question6Question7Question8Question9Question10Question11Question12Question13Question14Question15Question16Question17Question18Question19Question20Question21Question22Question23Question24Question25Question26Question27Question28Question29Question30Question31Question32Question33Question34Question35Question36Question37Question38Question39Question40Question41Question42Question43Question44Question45Question46Question47Question48Question49Question50Question51Question52Question53Question54
21234Row1.01Row1.02Row1.03Row1.04Row1.05Row1.06Row1.07Row1.08Row1.09Row1.10Row1.11Row1.12Row1.13Row1.14Row1.15Row1.16Row1.17Row1.18Row1.19Row1.20Row1.21Row1.22Row1.23Row1.24Row1.25Row1.26Answer1Answer2Answer3Answer4Answer5Answer6Answer7Answer8Answer9Answer10Answer11Answer12Answer13Answer14Answer15Answer16Answer17Answer18Answer19Answer20Answer21Answer22Answer23Answer24Answer25Answer27Answer28Answer29Answer30Answer31Answer32Answer33Answer34Answer35Answer36Answer37Answer38Answer39Answer40Answer42Answer43Answer45Answer46Answer47Answer48Answer49Answer50Answer51Answer52Answer53Answer54
3Answer55Answer56Answer57Answer58Answer59Answer60Answer62Answer64Answer65Answer66Answer68Answer69Answer70Answer71Answer72Answer73Answer74Answer75Answer76Answer77Answer78Answer79Answer80Answer81Answer82Answer83Answer84Answer85Answer87Answer88Answer89Answer90Answer91Answer92Answer93Answer94Answer95Answer96Answer97Answer98Answer99Answer100Answer101Answer102Answer104Answer105Answer107Answer108
Data


Second WO# row left alone
Macro.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQAR
561235Row2.01Row2.02Row2.03Row2.04Row2.05Row2.06Row2.07Row2.08Row2.09Row2.10Row2.11Row2.12Row2.13Row2.14Row2.15Row2.16Row2.17Row2.18Row2.19Row2.20Row2.21Row2.22Row2.23Row2.24Row2.25Row2.26
Data
 
Upvote 0
Sorry, I'm not sure what you are saying.

The following is the data that I m starting with:
Part 1:
Book2
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1WO#Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26QuestionAnswer
21234Row2.01Row2.02Row2.03Row2.04Row2.05Row2.06Row2.07Row2.08Row2.09Row2.10Row2.11Row2.12Row2.13Row2.14Row2.15Row2.16Row2.17Row2.18Row2.19Row2.20Row2.21Row2.22Row2.23Row2.24Row2.25Row2.26Question1Answer1
35678Row3.01Row3.02Row3.03Row3.04Row3.05Row3.06Row3.07Row3.08Row3.09Row3.10Row3.11Row3.12Row3.13Row3.14Row3.15Row3.16Row3.17Row3.18Row3.19Row3.20Row3.21Row3.22Row3.23Row3.24Row3.25Row3.26Question2Answer2
Original


Part2:
Book2
ABACAD
1QuestionAnswer
2Question1Answer1
3Question2Answer2
4Question3Answer3
5Question4Answer4
6Question5Answer5
7Question6Answer6
8Question7Answer7
9Question8Answer8
10Question9Answer9
11Question10Answer10
12Question11Answer11
13Question12Answer12
14Question13Answer13
15Question14Answer14
16Question15Answer15
17Question16Answer16
18Question17Answer17
19Question18Answer18
20Question19Answer19
21Question20Answer20
22Question21Answer21
23Question22Answer22
24Question23Answer23
25Question24Answer24
26Question25Answer25
27Question26
28Question27Answer27
29Question28Answer28
30Question29Answer29
31Question30Answer30
32Question31Answer31
33Question32Answer32
34Question33Answer33
35Question34Answer34
36Question35Answer35
37Question36Answer36
38Question37Answer37
39Question38Answer38
40Question39Answer39
41Question40Answer40
42Question41
43Question42Answer42
44Question43Answer43
45Question44
46Question45Answer45
47Question46Answer46
48Question47Answer47
49Question48Answer48
50Question49Answer49
51Question50Answer50
52Question51Answer51
53Question52Answer52
54Question53Answer53
55Question54Answer54
56Question1AAnswer1A
57Question2AAnswer2A
58Question3AAnswer3A
59Question4AAnswer4A
60Question5AAnswer5A
61Question6AAnswer6A
62Question7AAnswer7A
63Question8AAnswer8A
64Question9AAnswer9A
65Question10AAnswer10A
66Question11AAnswer11A
67Question12AAnswer12A
68Question13AAnswer13A
69Question14AAnswer14A
70Question15AAnswer15A
71Question16AAnswer16A
72Question17AAnswer17A
73Question18AAnswer18A
74Question19AAnswer19A
75Question20AAnswer20A
76Question21AAnswer21A
77Question22AAnswer22A
78Question23AAnswer23A
79Question24AAnswer24A
80Question25AAnswer25A
81Question26AAnswer26A
82Question27AAnswer27A
83Question28AAnswer28A
84Question29AAnswer29A
85Question30AAnswer30A
86Question31AAnswer31A
87Question32AAnswer32A
88Question33AAnswer33A
89Question34AAnswer34A
90Question35AAnswer35A
91Question36AAnswer36A
92Question37AAnswer37A
93Question38AAnswer38A
94Question39AAnswer39A
95Question40AAnswer40A
96Question41A
97Question42AAnswer42A
98Question43AAnswer43A
99Question44A
100Question45AAnswer45A
101Question46AAnswer46A
102Question47AAnswer47A
103Question48AAnswer48A
104Question49AAnswer49A
105Question50AAnswer50A
106Question51AAnswer51A
107Question52AAnswer52A
108Question53AAnswer53A
109Question54AAnswer54A
110
Original



The reult I get after running the code is:
Book2
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACBCCCD
1WO#Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11Header12Header13Header14Header15Header16Header17Header18Header19Header20Header21Header22Header23Header24Header25Header26Question1Question2Question3Question4Question5Question6Question7Question8Question9Question10Question11Question12Question13Question14Question15Question16Question17Question18Question19Question20Question21Question22Question23Question24Question25Question26Question27Question28Question29Question30Question31Question32Question33Question34Question35Question36Question37Question38Question39Question40Question41Question42Question43Question44Question45Question46Question47Question48Question49Question50Question51Question52Question53Question54
21234Row2.01Row2.02Row2.03Row2.04Row2.05Row2.06Row2.07Row2.08Row2.09Row2.10Row2.11Row2.12Row2.13Row2.14Row2.15Row2.16Row2.17Row2.18Row2.19Row2.20Row2.21Row2.22Row2.23Row2.24Row2.25Row2.26Answer1Answer2Answer3Answer4Answer5Answer6Answer7Answer8Answer9Answer10Answer11Answer12Answer13Answer14Answer15Answer16Answer17Answer18Answer19Answer20Answer21Answer22Answer23Answer24Answer25Answer27Answer28Answer29Answer30Answer31Answer32Answer33Answer34Answer35Answer36Answer37Answer38Answer39Answer40Answer42Answer43Answer45Answer46Answer47Answer48Answer49Answer50Answer51Answer52Answer53Answer54
35678Row3.01Row3.02Row3.03Row3.04Row3.05Row3.06Row3.07Row3.08Row3.09Row3.10Row3.11Row3.12Row3.13Row3.14Row3.15Row3.16Row3.17Row3.18Row3.19Row3.20Row3.21Row3.22Row3.23Row3.24Row3.25Row3.26Answer1AAnswer2AAnswer3AAnswer4AAnswer5AAnswer6AAnswer7AAnswer8AAnswer9AAnswer10AAnswer11AAnswer12AAnswer13AAnswer14AAnswer15AAnswer16AAnswer17AAnswer18AAnswer19AAnswer20AAnswer21AAnswer22AAnswer23AAnswer24AAnswer25AAnswer26AAnswer27AAnswer28AAnswer29AAnswer30AAnswer31AAnswer32AAnswer33AAnswer34AAnswer35AAnswer36AAnswer37AAnswer38AAnswer39AAnswer40AAnswer42AAnswer43AAnswer45AAnswer46AAnswer47AAnswer48AAnswer49AAnswer50AAnswer51AAnswer52AAnswer53AAnswer54A
4
Data


Is your data arranged differently? Please further explain what the code I submitted is not doing for you.
 
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

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