Add headers for each range within a table from the first row to the last row of the same repeating element

leap out

Active Member
Joined
Dec 4, 2020
Messages
271
Office Version
  1. 2016
  2. 2010
Hi

I want to add headers for each separate range and for each element, starting from the first row and ending with the last row for the same element, provided that the entire range is duplicated for the same element based on column A, and the headers must be the same as in the first row for all ranges, as in the second range, the headers are different For the headers of the first row of the first range, then the headers of the first row of the first range must be replaced with the headers of the second .as to third range should insert new row before the first row with a new element and after the last row of the element that precedes it, and the headings are the same as the headers of the first row for the first range
will be add new ranges , so the macro must be applied several times each time new ranges are added without affecting what was previously executed on some of the ranges that already existed.


before
items v .xlsm
ABCDEFGH
1CLASSIFICATIONGOODSMARKMANFACTUREBUYINGSELLINGNETNOTE
2OIL-AS2 MLO-1 10W40 208L TT/LCASSU23
3OIL-AS210W40 208LENIIT1211
4OIL-AS25W30 208LQ8EU
5OIL-AS210W40 208LQ8EU11
6ITEMBRANDSQPRTD4ENTEROUTTOTALDEL.SS
7OIL-AS310W40 12x1LQ8EU111
8OIL-AS310W40 12x1LCASSU111
9OIL-AS310W40 12x1LENIIT112
10OIL-AS3OL-115W40 12x1LCASSU**222
11MS-OIL15W30 12x1LQ8EU11
12MS-OIL110W40 4x4LQ8EU11111
13MS-OIL1OIL/M 10W40 4x4LCAS TRMSU N3-9033
14MS-OIL110W40 4x4L MNH/80-TENIIT L66RT44
15MS-OIL15W40 4x4LQ8EU555
16MS-OIL15W40 4x4LCASSU555
17MS-OIL15W40 4x4LENIIT555
18MS-OIL120W50 4x4LQ8EU222
19MS-OIL120W50 4x4LCASSU222
20MS-OIL120W50 4x4LENIIT222
21MS-OIL15W30 4x4LQ8EU555
5




after
items v .xlsm
ABCDEFGH
1CLASSIFICATIONGOODSMARKMANFACTUREBUYINGSELLINGNETNOTE
2OIL-AS2 MLO-1 10W40 208L TT/LCASSU23
3OIL-AS210W40 208LENIIT1211
4OIL-AS25W30 208LQ8EU
5OIL-AS210W40 208LQ8EU11
6CLASSIFICATIONGOODSMARKMANFACTUREBUYINGSELLINGNETNOTE
7OIL-AS310W40 12x1LQ8EU111
8OIL-AS310W40 12x1LCASSU111
9OIL-AS310W40 12x1LENIIT112
10OIL-AS3OL-115W40 12x1LCASSU**222
11CLASSIFICATIONGOODSMARKMANFACTUREBUYINGSELLINGNETNOTE
12MS-OIL15W30 12x1LQ8EU11
13MS-OIL110W40 4x4LQ8EU11111
14MS-OIL1OIL/M 10W40 4x4LCAS TRMSU N3-9033
15MS-OIL110W40 4x4L MNH/80-TENIIT L66RT44
16MS-OIL15W40 4x4LQ8EU555
17MS-OIL15W40 4x4LCASSU555
18MS-OIL15W40 4x4LENIIT555
19MS-OIL120W50 4x4LQ8EU222
20MS-OIL120W50 4x4LCASSU222
21MS-OIL120W50 4x4LENIIT222
22MS-OIL15W30 4x4LQ8EU555
5
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi!

This should work for you:
VBA Code:
Sub test()
  Dim lRow As Long, i As Long
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  With ActiveSheet
    .AutoFilterMode = False
    With Range("A1:A" & lRow)
        .AutoFilter 1, "ITEM"
        On Error Resume Next
        .Offset(1).EntireRow.Delete
    End With
    .AutoFilterMode = False
  On Error GoTo 0
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 3 Step -1
    If Cells(i, 1).Value <> Cells(i - 1, 1).Value Then
      Rows(1).Copy
      Rows(i).EntireRow.Insert
    End If
  Next
  Application.ScreenUpdating = True
  End With
End Sub
 
Upvote 0
thanks
gives error Autofilter method of range class failed in this line
VBA Code:
        .AutoFilter 1, "ITEM"

 
Upvote 0
See this file is working. I am not sharing the code because it is exactly the same as post#2.
 
Upvote 0
strange!
I should check again what's the problem
ok but there is problem after run the macro and add new data should not add headers again for data have already added.
 
Upvote 0
Then change this line:
VBA Code:
If Cells(i, 1).Value <> Cells(i - 1, 1).Value Then

to this:
VBA Code:
If Cells(i, 1).Value <> Cells(i - 1, 1).Value And Cells(i - 1, 1).Value <> "CLASSIFICATION" Then
 
Upvote 0
thanks again
I should check again what's the problem
now I see the problem . you use normal range but I use Table .
do you have any idea how do that with Table?
 
Upvote 0
How about like this? It converts the table to dynamic range then converts back to table again:
VBA Code:
Sub test()
  Dim lRow As Long, i As Long
  Dim rList As Range
 
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  With ActiveSheet
  Set rList = .ListObjects("Table1").Range 'your table name
  .ListObjects(1).Unlist 'convert to range
  .AutoFilterMode = False
  With Range("A1:A" & lRow)
  .AutoFilter 1, "ITEM"
  On Error Resume Next
  .Offset(1).EntireRow.Delete
  End With
  .AutoFilterMode = False
  On Error GoTo 0
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 3 Step -1
    If Cells(i, 1).Value <> Cells(i - 1, 1).Value And Cells(i - 1, 1).Value <> "CLASSIFICATION" Then
      Rows(1).Copy
      Rows(i).EntireRow.Insert
    End If
  Next
  Application.ScreenUpdating = True
  .ListObjects.Add(xlSrcRange, Range([A1].End(xlDown), [A1].End(xlToRight)), , xlYes).Name = "Table1" 'convert back to table
  End With
End Sub
 
Upvote 0
perfect !
can you fix as you did it in post#6 ?
because it will repeat copy the headers , despite of the same line is existed in the code as you did it in post#6:confused:
 
Upvote 0

Forum statistics

Threads
1,215,260
Messages
6,123,926
Members
449,135
Latest member
NickWBA

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