rearranging data based on headers and get rid of merged cells

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hi
I search for macro to arrange data across sheets based on headers(TOTAL,DATE,INVOICE NO,UNIT PRICE,QTY,CODE,BRAND), (MOVEMENT : PURCHASING,MOVEMENT : SELLING) and get rid of merged cells
alaways data start from row 8 for first sheet and others sheets start from row4
original data in all of sheets
KashfMabiatReport1.xls
ABCDEFGHIJKL
8MOVEMENT : PURCHASING
9TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
10242,560.0002024.01.02114895.000801284GC 1200R20 AZ0026 CHI
11925.000401285GC 1200R20 AZ0183 CHI
12735.000201385GC 315/80R22.5 AT161 CHI
13735.000201287GC 315/80R22.5 AZ126 CHI
14745.000201294GC 315/80R22.5 AZ188 CHI
152,035.000401241BS 1200R20 G580 JAP
16425.00081227BS 215/70R15C R623 THI
17405.000121221BS 205/70R15C R623 THI
18TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
19114,840.0002024.01.09121715.000801306BS 750R16 R230 JAP
20515.000401310BS 225/95R16C D618 JAP
21535.000501391BS 265/65R17 D840 JAP
22735.000141287GC 315/80R22.5 AZ126 CHI
23TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
24850.0002024.01.10125425.00021227BS 215/70R15C R623 THI
25TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
265,210.0002024.01.17138570.00051401BS 650R16 R230 JAP
27590.00041402BS 245/70R16 D697 JAP
28TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
2941,550.0002024.01.20148935.000241292GC 1200R24 AZ166 CHI
30735.000261287GC 315/80R22.5 AZ126 CHI
31TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
32411,770.0002024.01.20149405.000501221BS 205/70R15C R623 THI
33425.000121227BS 215/70R15C R623 THI
34505.000241346BS 255/70R15C D840
352,035.0001801241BS 1200R20 G580 JAP
362,000.00041244BS 1200R20 R187 JAP
37TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
3811,205.0002024.01.21280705.00011190BS 285/50R20 DSPORT JAP
39725.00041411BS 275/55R20 ALENZA1 JAP
401,900.00041269BS 1200R24 G580 JAP
41TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
429,400.0002024.03.26215940.000101306BS 750R16 R230 JAP
Page 0




KashfMabiatReport1.xls
ABCDEFGHIJ
4TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
516,557.0002024.06.26336690.00051391BS 265/65R17 D840 JAP
6942.00041411BS 275/55R20 ALENZA1 JAP
7544.000161227BS 215/70R15C R623 THI
8635.00011346BS 255/70R15C D840
9TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
1024,884.0002024.07.03347635.00081346BS 255/70R15C D840
11625.00041547BS 205R16C D840 THI
12721.000241326BS 265/60R18 D840 JAP
13
14MOVEMENT : SELLING
15TOTALDATEINVOICE NOUNIT PRICEQTYCODEBRAND
167,750.0002024.01.24561775.000101306BS 750R16 R230 JAP
17
18SUMMARY
191,891,459.000TOTAL PURCHASE
207,750.000TOTAL SELLING
212,120,485.000TOTAL PAID
Page 3


result in INVOICES sheet
KashfMabiatReport1.xls
ABCDEFG
3MOVEMENT : PURCHASING
4DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
52024.01.021141284GC 1200R20 AZ0026 CHI80895.000242,560.000
61285GC 1200R20 AZ0183 CHI40925.000
71385GC 315/80R22.5 AT161 CHI20735.000
81287GC 315/80R22.5 AZ126 CHI20735.000
91294GC 315/80R22.5 AZ188 CHI20745.000
101241BS 1200R20 G580 JAP402,035.000
111227BS 215/70R15C R623 THI8425.000
121221BS 205/70R15C R623 THI12405.000
13DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
142024.01.091211306BS 750R16 R230 JAP80715.000114,840.000
151310BS 225/95R16C D618 JAP40515.000
161391BS 265/65R17 D840 JAP50535.000
171287GC 315/80R22.5 AZ126 CHI14735.000
18DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
192024.01.101251227BS 215/70R15C R623 THI2425.000850.000
20DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
212024.01.171381401BS 650R16 R230 JAP5570.0005,210.000
221402BS 245/70R16 D697 JAP4590.000
23DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
242024.01.201481292GC 1200R24 AZ166 CHI24935.00041,550.000
251287GC 315/80R22.5 AZ126 CHI26735.000
26DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
272024.01.201491221BS 205/70R15C R623 THI50405.000411,770.000
281227BS 215/70R15C R623 THI12425.000
291346BS 255/70R15C D84024505.000
301241BS 1200R20 G580 JAP1802,035.000
311244BS 1200R20 R187 JAP42,000.000
32DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
332024.01.212801190BS 285/50R20 DSPORT JAP1705.00011,205.000
341411BS 275/55R20 ALENZA1 JAP4725.000
351269BS 1200R24 G580 JAP41,900.000
36DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
372024.03.262151306BS 750R16 R230 JAP10940.0009,400.000
38DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
392024.06.263361391BS 265/65R17 D840 JAP5690.00016,557.000
401411BS 275/55R20 ALENZA1 JAP4942.000
411227BS 215/70R15C R623 THI16544.000
421346BS 255/70R15C D8401635.000
43DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
442024.07.033471346BS 255/70R15C D8408635.00024,884.000
451547BS 205R16C D840 THI4625.000
461326BS 265/60R18 D840 JAP24721.000
47MOVEMENT : SELLING
48DATEINVOICE NOCODEBRANDQTYUNIT PRICETOTAL
492024.01.245611306BS 750R16 R230 JAP10775.0007,750.000
50
51
52SUMMARY
53TOTAL PURCHASE1,891,459.000
54TOTAL SELLING7,750.000
55TOTAL PAID2,120,485.000
56
INVOICES

I can't post the others sheets because XL2BB tool prohibit for much character
thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this macro. Works on active sheet.
VBA Code:
Sub Rearrange_Data()
Dim cnt&, T&, S$
Dim K As Range
    With ActiveSheet.UsedRange
    cnt = .Columns.Count
    .UnMerge
    For T = 1 To cnt
    Set K = .Columns(T).Find("*")
    If K Is Nothing Then S = S & "," & .Cells(1, T).Address
    Next T
    End With
   
    If S <> "" Then Range(Mid(S, 2)).EntireColumn.Delete
   
End Sub
 
Upvote 0
thanks
your code just delete merged cells without change arranging columns again as I put in result !
 
Upvote 0
Try this code. I have verified with your file. Working OK.
VBA Code:
Sub Rearrange_Data1()
Dim cnt&, clm&, T&, S$, K&, X&, m&
Dim Rng As Range, Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range
Dim Sh As Worksheet
Dim Lastsh$

Sheets("INVOICES").Cells.Clear
For Each Sh In Worksheets
If Sh.Name = "Page 0" Then
K = 8
ElseIf Left(Sh.Name, 4) = "Page" Then
K = 4
Else
K = 0
End If

If K <> 0 Then
With Sh.Range("A" & K)
clm = 0
    cnt = Sh.Cells(K + 1, Columns.Count).End(xlToLeft).Column
    With .CurrentRegion
    .UnMerge
    For T = 1 To cnt
    clm = clm + 1
    Set Rng = .Columns(clm).Find("*")
    If Rng Is Nothing Then
    .Columns(clm).Delete Shift:=xlToLeft
    clm = clm - 1
    End If
    Next T
    End With
    X = Sheets("INVOICES").Range("D" & Rows.Count).End(xlUp).Row
    .CurrentRegion.Copy Sheets("INVOICES").Range("A" & X + 1)
 End With
Lastsh = Sh.Name
End If
Next Sh

With Sheets(Lastsh)
Set Rng1 = .Cells.Find("SELLING")
If Not Rng1 Is Nothing Then
    clm = 0
    m = Rng1.Row + 2
    cnt = .Cells(m, Columns.Count).End(xlToLeft).Column
    
    With .Cells(Rng1.Row, 1).CurrentRegion
    .UnMerge
    For T = 1 To cnt
    clm = clm + 1
    Set Rng2 = .Columns(clm).Find("*")
    If Rng2 Is Nothing Then
    .Columns(clm).Delete Shift:=xlToLeft
    clm = clm - 1
    End If
    Next T
    End With
    X = Sheets("INVOICES").Range("D" & Rows.Count).End(xlUp).Row
    .Cells(Rng1.Row, 1).CurrentRegion.Copy Sheets("INVOICES").Range("A" & X + 1)
   ' End With

End If
End With

With Sheets("INVOICES").Range("A1").CurrentRegion
.Columns(5).Copy .Cells(1, .Columns.Count + 1)
.Columns(4).Copy .Cells(1, .Columns.Count + 2)
.Columns(1).Copy .Cells(1, .Columns.Count + 3)
.Columns(5).Delete Shift:=xlToLeft
.Columns(4).Delete Shift:=xlToLeft
.Columns(1).Delete Shift:=xlToLeft
.Cells(2, 7).Cut .Cells(2, 4)
.Cells(X + 1, 7).Cut .Cells(X + 1, 4)
.WrapText = False
End With

With Sheets(Lastsh)
Set Rng3 = .Cells.Find("SUMMARY")
If Not Rng3 Is Nothing Then
    clm = 0
    m = Rng3.Row + 2
    cnt = .Cells(m, Columns.Count).End(xlToLeft).Column
    With .Cells(Rng3.Row, 1).CurrentRegion
    .UnMerge
    For T = 1 To cnt
    clm = clm + 1
    Set Rng4 = .Columns(clm).Find("*")
    If Rng4 Is Nothing Then
    .Columns(clm).Delete Shift:=xlToLeft
    clm = clm - 1
    End If
    Next T
    End With
    X = Sheets("INVOICES").Range("D" & Rows.Count).End(xlUp).Row
    .Cells(Rng3.Row, 1).CurrentRegion.Copy Sheets("INVOICES").Range("A" & X + 3)

End If
End With

With Sheets("INVOICES").Range("A" & X + 3).CurrentRegion
.Cells(1, 1).Copy .Cells(1, 2)
.Columns(1).Offset(1, 0).Copy .Cells(2, .Columns.Count + 1)
.Columns(1).Delete Shift:=xlToLeft
.WrapText = False
End With

Sheets("INVOICES").UsedRange.EntireColumn.AutoFit  '.Columns("A:K")

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,313
Members
452,634
Latest member
cpostell

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