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
288
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
OK,

Fix the same line like this:
VBA Code:
If Cells(i, 1).Value <> Cells(i - 1, 1).Value And Cells(i, 1).Value <> "CLASSIFICATION" And Cells(i - 1, 1).Value <> "CLASSIFICATION" Then
 
Upvote 0
excellent!

just question . based on your code seems to identify ITEM word but if I have another range contains headers doesn't contain ITEM word then will not work correctly . so the question is there way doesn't depend on specific word especially if I have many headers don't contain ITEM word in (different words in column A for the header for each range)?
 
Upvote 0
like this
Sample (1).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
14S.NCODEBATCHIT CODEINPUTOUTNETREF
15MS-OIL210W40 4x4L MNH/80-TENIIT L66RT44
16MS-OIL25W40 4x4LQ8EU555
17I.SIDOR-FGMTR-BRBT QIMPEXDES
18MS-OIL45W40 4x4LCASSU555
Sayfa3

How can we differentiate regular rows from headers?
just I ask you what do you think., what the better do that.
Is there any identifier?
do you mean we need helper column contains the headers in column A to match and do that?
 
Upvote 0
This should work if this is the same shade of gray in XL2BB. You must play with RGB value if it doesn't work.
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, RGB(166, 166, 166), xlFilterCellColor
  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).Value <> "CLASSIFICATION" 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 1
Solution

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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