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

jazzhayward

New Member
Joined
Sep 16, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
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.

spreadsheet screenshot 160922 - Copy.png


First I need just one version of the data in P2:P22 to copies and transposed to R1 to provide headings.
Then need Q2:22 to be transposed to R2. I then need rows 3:22 to be deleted.
Then need to repeat the same for the next invoice number (which is in column A) but it will be on the next row down (Q3:23 transposed to R3, then delete 4:23 to be deleted and so on).

As mentioned this file will have varying rows so need to make sure it captures everything. I thought it might be a mix of an IF formula and transpose but it's a bit beyond what I can get my head around.

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

Thank you in advance!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
It is hard to work with a picture. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
It is hard to work with a picture. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).

Hi Mumps,

Thanks for the tips.

Please see below excerpt:

Copy of SWAU data 160922 - truncated - EDITED.xlsx
ABCDEFGHIJKLMNOPQ
1Transaction NumberTransaction SourceTransaction ClassTransaction TypeCompleteBill-to CustomerBill-to Customer Account NumberTransaction DateBusiness UnitOriginal Transaction NumberEntered AmountCurrencyDue DatePO NumberReferenceDetail Data
251615501A1cars+InvoiceRental InvoiceYesXXXX AUSTRALIASWAU03-Sep-2022XXXX NEW ZEALAND974.76NZD03-Oct-2022123456789Context Valuecars+
3Pay Type1234xyz
4Rate1234abc
5Driver NameJOHN SMITH
6Pickup Location Code and DescMEL12 MELBOURNE EARTH
7Pickup Date and Time28-AUG-2022 13:24:00
8DropOff Location Code and DescMEL12 MELBOURNE EARTH
9Drop Off Date and Time03-SEP-2022 14:20:00
10Vehicle ChargedK
11Car Reg. NoVEH123
12Vehicle SuppliedRED CLOWN CAR
13Kms Out13008
14Kms In13457
15Carbon Emissions76330.00
16Location Owner NameXXXX NEW ZEALAND LTD
17Location Owner GST No.12-345-XXX
18Context Valuecars+
19RA Number123456789
20Reservation NumberK12345
21Voucher NumberZXYZ123
22Frequent Flyer
2351622054A1cars+InvoiceRental InvoiceYesXXXX AUSTRALIASWAU12-Sep-2022XXXX NEW ZEALAND487.70NZD12-Oct-2022123456789Context Valuecars+
24Pay Type1234xyz
25Rate1234abc
26Driver NameJOHN SMITH
27Pickup Location Code and DescMEL12 MELBOURNE EARTH
28Pickup Date and Time07-SEP-2022 18:09:00
29DropOff Location Code and DescMEL12 MELBOURNE EARTH
30Drop Off Date and Time12-SEP-2022 14:10:00
31Vehicle ChargedF2
32Car Reg. NoVEH123
33Vehicle SuppliedRED CLOWN CAR
34Kms Out9447
35Kms In9954
36Carbon Emissions48672.00
37Location Owner NameXXXX NEW ZEALAND LTD
38Location Owner GST No.12-345-XXX
39Context Valuecars+
40RA Number123456789
41Reservation NumberK12345
42Voucher NumberZXYZ123
43Frequent Flyer
4451623800A1cars+InvoiceRental InvoiceYesXXXX AUSTRALIASWAU15-Sep-2022XXXX NEW ZEALAND396.00NZD15-Oct-2022123456789Context Valuecars+
45Pay Type1234xyz
46Rate1234abc
47Driver NameJOHN SMITH
48Pickup Location Code and DescMEL12 MELBOURNE EARTH
49Pickup Date and Time11-SEP-2022 16:42:00
50DropOff Location Code and DescMEL12 MELBOURNE EARTH
51Drop Off Date and Time15-SEP-2022 15:42:00
52Vehicle ChargedJ
53Car Reg. NoVEH123
54Vehicle SuppliedRED CLOWN CAR
55Kms Out326
56Kms In585
57Carbon Emissions53354.00
58Location Owner NameXXXX NEW ZEALAND LTD
59Location Owner GST No.12-345-XXX
60Context Valuecars+
61RA Number123456789
62Reservation NumberK12345
63Voucher NumberZXYZ123
64Frequent FlyerHERTZ REWARDS
6551623585A1cars+InvoiceRental InvoiceYesXXXX AUSTRALIASWAU14-Sep-2022XXXX NEW ZEALAND570.36NZD14-Oct-2022123456789Context Valuecars+
66Pay Type1234xyz
67Rate1234abc
68Driver NameJOHN SMITH
69Pickup Location Code and DescMEL12 MELBOURNE EARTH
70Pickup Date and Time11-SEP-2022 10:25:00
71DropOff Location Code and DescMEL12 MELBOURNE EARTH
72Drop Off Date and Time14-SEP-2022 09:30:00
73Vehicle ChargedG
74Car Reg. NoVEH123
75Vehicle SuppliedRED CLOWN CAR
76Kms Out2610
77Kms In3081
78Carbon Emissions80070.00
79Location Owner NameXXXX NEW ZEALAND LTD
80Location Owner GST No.12-345-XXX
81Context Valuecars+
82RA Number123456789
83Reservation NumberK12345
84Voucher NumberZXYZ123
85Frequent Flyer
Export to Excel (2)


As mentioned this file will have varying rows so need to make sure it captures everything. I thought it might be a mix of an IF formula and transpose but it's a bit beyond what I can get my head around.

As mentioned on my original post:
1) Copy data from P2:P22 and transpose to R1 to provide the headings for the the data (this only needs to happen once)
2) For the invoice invoice number (in column A), copy data from Q2:Q22 and transpose to R2 (so it's inline with the invoice number in column A)
3) Delete rows 3:22
4) Then I need to repeat the above steps for the next invoice number (in column A) but the data copied will be Q3:23, transposed to R3, then delete rows 4:23)
5) I need this to be repeated all the way through the spreadsheet until it reaches the bottom. This spreadsheet will have a varying number of lines. The headings will always be the same but the data will be different.

I hope this has described the issue well, please let me know if you need more detail.

Thanks in advanced!
 
Upvote 0
Try:
VBA Code:
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, fnd1 As Range, sAddr As String, fRow As Long, x As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With Range("A2:A" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = 2 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            x = Range("A2:A" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
            Range("A" & fRow).Resize(, 15).Copy Range("A" & x)
        Next i
    End With
    Range("R1").Resize(, 21).Value = WorksheetFunction.Transpose(Range("P2:P22").Value)
    Set fnd1 = Range("P:P").Find("Pay Type", LookIn:=xlValues, LookAt:=xlWhole)
    If Not fnd1 Is Nothing Then
        sAddr = fnd1.Address
        Do
            Cells(Rows.Count, "R").End(xlUp).Offset(1).Resize(, 21).Value = WorksheetFunction.Transpose(Range("Q" & fnd1.Row - 1).Resize(21).Value)
            Set fnd1 = Range("P:P").Find(fnd1, after:=fnd1, LookIn:=xlValues, LookAt:=xlWhole)
        Loop While fnd1.Address <> sAddr
        sAddr = ""
    End If
    Rows(x + 1 & ":" & LastRow).Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Mumps,

Thank for you for the code. I copied it exactly and ran the macro and it works great until it gets to line 22. It looks like it may have grabbed an invoice number from line 2 again, then continued. I can't quite figure it out.

I'm not at home at the moment so can't insert the Excel snip so I'll add when I get home.

Thanks
 
Upvote 0
Hi Mumps,

I have put all 3 files in a link that you can look at:

Macro Testing Files

I have the original file, the macro file and then the file after the macro was applied. I have highlighted the errors in orange/yellow.

Hope you can figure it out.

Thanks in advance
 
Upvote 0
Try:
VBA Code:
Sub TransposeData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, fnd1 As Range, sAddr As String, fRow As Long, x As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With Range("A2:A" & LastRow).SpecialCells(xlCellTypeConstants)
        For i = 2 To .Areas.Count
            fRow = .Areas(i).Cells(1).Row
            x = Range("A2:A" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
            Range("A" & fRow).Resize(, 15).Cut Range("A" & x)
        Next i
    End With
    Range("R1").Resize(, 21).Value = WorksheetFunction.Transpose(Range("P2:P22").Value)
    Set fnd1 = Range("P:P").Find("Pay Type", LookIn:=xlValues, LookAt:=xlWhole)
    If Not fnd1 Is Nothing Then
        sAddr = fnd1.Address
        Do
            Cells(Rows.Count, "R").End(xlUp).Offset(1).Resize(, 21).Value = WorksheetFunction.Transpose(Range("Q" & fnd1.Row - 1).Resize(21).Value)
            Set fnd1 = Range("P:P").Find(fnd1, after:=fnd1, LookIn:=xlValues, LookAt:=xlWhole)
        Loop While fnd1.Address <> sAddr
        sAddr = ""
    End If
    Rows(x + 1 & ":" & LastRow).Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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