Type Mismatch When deleting Duplicate Headers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have headers in Col A with text "Reference"

I want to delete the rows containing text "Reference" except the first row containing this


However when run my macro, I get Type mismatch

Code:
 Sub Del_Unwanted()
Dim LR As Long, i As Long
With Sheets("Data Import")

LR = .Range("A" & .Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
If .Range("A" & i).Value = "Reference" Then .Range("A" & i).EntireRow.Delete
Next i

.UsedRange.EntireColumn.AutoFit
End With
Columns("L:L").ColumnWidth = 64.86
End Sub

It would be appreciated if someone could kindly amend my code
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Very strange, in my excel this code can run normally
You can try changing:
VBA Code:
. Range ("a" & I). [COLOR=rgb(184, 49, 47)]value[/COLOR]
to:
VBA Code:
. Range ("a" & I). [COLOR=rgb(184, 49, 47)]text[/COLOR]

Maybe you should also check the format(number) of column A
 
Last edited:
Upvote 0
In addition, this is the data I use to test the code
test.xlsm
AB
1
2A
3B
4C
5D
6Reference
7F
8G
9Reference
10I
11J
12K
13Reference
14M
15N
16Reference
17P
Data Import
 
Upvote 0
I changed .value to .text and it now works
 
Upvote 0
@hnsd24_CN
If you want to apply your own format to vba code, use the RICH code tags.
1603540308601.png
 
Upvote 0

Forum statistics

Threads
1,214,518
Messages
6,119,996
Members
448,935
Latest member
ijat

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