add missed headers for different location based on above rows

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
573
Office Version
  1. 2019
Hi
I have sheet contains invoices details but some rows don't contain headers(TOTAL,DATE,INVOICE NO,PRICE,QTY,ID NO,DESCRIBE)
so I would macro to add the headers based on formatting row where precede it.
bfore rows 25,54
arranging data.xlsm
ABCDEFGHIJKLMNOPQRST
1BRIDD
2
3Analytical detection of a client's movement
4
5
6
72024.05.08
8
9
10NAME: ABSI
11
12
13MOVEMENT TYPE :SELLING
14PAGE NO (1 ) TO (2)
15TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
161,700.002024.01.0950742541390KM 185/70R14 TA21 KOR
17
18
19TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
203,965.002024.01.1051175041394DUNLOP 265/60R18 AT22 JAP
2147511392CRESTAL 90A L KOR
2249011367CRESTAL 100A KOR
23
24
25
262,550.002024.01.1351547521392CRESTAL 90A L KOR
2750021348DONGA 100 A KOR
2860011395BATTERY 88A KORE
29
30
31TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
323,400.002024.01.1452068051371DUNLOP 265/65R17 AT20 JAP
33
34NAME: ABSIPAGE NO (2 ) TO (2)
35
36TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
371,090.002024.01.2355854521412MARSHAL 245/70R17 KL61KOR
38
39
40TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
413,400.002024.01.2355985041411BS 275/55R20 ALENZA1 JAP
42
43
44TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
455,940.002024.01.2556242541414GOODRIDE 255/70R15 CHI
461,060.0041417CONTINENTAL 255/45R19 SC5GER
47
48
49MOVEMENT TYPE :SELLING RETURNING
50
51TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
523,000.002024.01.2811175041394DUNLOP 265/60R18 AT22 JAP
53
54
55425.002024.01.2811242511414GOODRIDE 255/70R15 CHI
KS


after for row 25,54

arranging data.xlsm
ABCDEFGHIJKLMNOPQRST
1BRIDD
2
3Analytical detection of a client's movement
4
5
6
72024.05.08
8
9
10NAME: ABSI
11
12
13MOVEMENT TYPE :SELLING
14PAGE NO (1 ) TO (2)
15TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
161,700.002024.01.0950742541390KM 185/70R14 TA21 KOR
17
18
19TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
203,965.002024.01.1051175041394DUNLOP 265/60R18 AT22 JAP
2147511392CRESTAL 90A L KOR
2249011367CRESTAL 100A KOR
23
24
25TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
262,550.002024.01.1351547521392CRESTAL 90A L KOR
2750021348DONGA 100 A KOR
2860011395BATTERY 88A KORE
29
30
31TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
323,400.002024.01.1452068051371DUNLOP 265/65R17 AT20 JAP
33
34NAME: ABSIPAGE NO (2 ) TO (2)
35
36TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
371,090.002024.01.2355854521412MARSHAL 245/70R17 KL61KOR
38
39
40TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
413,400.002024.01.2355985041411BS 275/55R20 ALENZA1 JAP
42
43
44TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
455,940.002024.01.2556242541414GOODRIDE 255/70R15 CHI
461,060.0041417CONTINENTAL 255/45R19 SC5GER
47
48
49MOVEMENT TYPE :SELLING RETURNING
50
51TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
523,000.002024.01.2811175041394DUNLOP 265/60R18 AT22 JAP
53
54TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
55425.002024.01.2811242511414GOODRIDE 255/70R15 CHI
KS
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
try this on a copy of your worksheet

VBA Code:
Sub AddHeaders()
    Dim rw As Long, col As Long, a, ckrow As Long, found As Boolean
    a = ActiveSheet.UsedRange
    For rw = 15 To UBound(a, 1)
        For col = 1 To UBound(a, 2)
            If (IsNumeric(a(rw, col)) Or IsNumeric(Left(a(rw, col), 6))) And Len(a(rw, col)) > 0 And Len(a(rw - 1, col)) = 0 Then
                ckrow = rw - 2
                found = False
                Do While ckrow > 14 And Not found
                    If Len(a(ckrow, col)) > 0 And Not (IsNumeric(a(ckrow, col)) Or IsNumeric(Left(a(ckrow, col), 6))) Then
                        found = True
                        Cells(rw - 1, col) = a(ckrow, col)
                    End If
                    ckrow = ckrow - 1
                Loop
            End If
        Next
    Next
    For rw = 15 To UBound(a, 1)
        For col = UBound(a, 2) To 6 Step -1
            If Len(a(rw, col)) > 0 And Len(a(rw - 1, col)) = 0 Then
                If a(rw, col) <> "DESCRIBE" And Left(a(rw, col), 4) <> "PAGE" Then Cells(rw - 1, col) = "DESCRIBE"
                Exit For
            End If
        Next
    Next
End Sub
 
Upvote 0
thanks
just I note if I have PAGE NO.... and NAME:... before detail doesn't contain header will fill NAME and PAGE NO !!
like this

arranging data.xlsm
ABCDEFGHIJKLM
36TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
371,090.002024.01.2355854521412MARSHAL 245/70R17 KL61KOR
38
39NAME: ABSIPAGE NO (2 ) TO (2)
40
413,400.002024.01.2355985041411BS 275/55R20 ALENZA1 JAP
KS


error result as highlighted(this case is rare for my real project)
arranging data.xlsm
ABCDEFGHIJKLM
36TOTALDATEINVOICE NOPRICEQTYID NODESCRIBE
371,090.002024.01.2355854521412MARSHAL 245/70R17 KL61KOR
38
39NAME: ABSIPAGE NO (2 ) TO (2)
40NAME: ABSIDATEINVOICE NOPAGE NO (2 ) TO (2)QTYID NODESCRIBE
413,400.002024.01.2355985041411BS 275/55R20 ALENZA1 JAP
KS

I hope there is way to fix it by exclude (NAME & PAGE NO).
 
Upvote 0
VBA Code:
Sub AddHeaders()
    Dim rw As Long, col As Long, a, ckrow As Long, found As Boolean
    a = ActiveSheet.UsedRange
    For rw = 15 To UBound(a, 1)
        For col = 1 To UBound(a, 2)
            If (IsNumeric(a(rw, col)) Or IsNumeric(Left(a(rw, col), 6))) And Len(a(rw, col)) > 0 And Len(a(rw - 1, col)) = 0 Then
                ckrow = rw - 2
                found = False
                Do While ckrow > 14 And Not found
                    If Len(a(ckrow, col)) > 0 And Not (IsNumeric(a(ckrow, col)) Or IsNumeric(Left(a(ckrow, col), 6))) Then
                        If Left(a(ckrow, col), 4) <> "NAME" And Left(a(ckrow, col), 4) <> "PAGE" Then
                            found = True
                            Cells(rw - 1, col) = a(ckrow, col)
                        End If
                    End If
                    ckrow = ckrow - 1
                Loop
            End If
        Next
    Next
    For rw = 15 To UBound(a, 1)
        For col = UBound(a, 2) To 6 Step -1
            If Len(a(rw, col)) > 0 And Len(a(rw - 1, col)) = 0 Then
                If a(rw, col) <> "DESCRIBE" And Left(a(rw, col), 4) <> "PAGE" Then Cells(rw - 1, col) = "DESCRIBE"
                Exit For
            End If
        Next
    Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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