Different no headers and this vba

fredhedge

New Member
Joined
Mar 18, 2019
Messages
2
Hi everybody

I have found this VBA and I want it to remove rows on sheet1 when its not found on sheet2. However, I have 1-row-header in sheet2 and 2-row-header in sheet1. I get the code only to work when both sheets have 1 row header. Can anybody of you please help me adjust the code?

Code:
[COLOR=#000000][FONT='inherit']Sub DeleteNotMatch22()[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Const sh1Col As String = "A"[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Const sh2Col As String = "A"[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Dim ws1 As Worksheet, ws2 As Worksheet[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Dim r1 As Long, r2 As Long, i As Long, x As Long[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Set ws1 = Sheets("Sheet1") 'This one has 2 row header.[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Set ws2 = Sheets("Sheet2") ' This one has 1 row header. [/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']r1 = ws1.Cells(Rows.Count, sh1Col).End(xlUp).Row[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']r2 = ws2.Cells(Rows.Count, sh2Col).End(xlUp).Row[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']On Error Resume Next[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']For i = 2 To r2[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']x = Application.Match(ws2.Cells(i, sh2Col), ws1.Range(sh1Col & "1:" & sh1Col & r1), 0)[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']ws1.Cells(x, 255) = "xx"[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Next i[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']ws1.Cells(1, 255) = "xx"[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']Intersect(ws1.UsedRange, ws1.Columns(255)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']ws1.Columns(255).ClearContents[/FONT][/COLOR]
[COLOR=#000000][FONT='inherit']End Sub[/FONT][/COLOR]
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
This is the only line that I saw that would make any difference about the headers.
Code:
x = Application.Match(ws2.Cells(i, sh2Col), ws1.Range(sh1Col & "[COLOR=#ff0000]3[/COLOR]:" & sh1Col & r1), 0)
Just change the row as shown in red above. Just remember that the Match function is returning the relative position of the value being seached for. Not the actual row number. So in this case the number returned for value of 'x' will be two more than the actual row number.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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