Type Miss Match error

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,132
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this cod and it sould delete the entire column or columns in the list.

cell A2 onwards will have a list of column headers which need to be deleted,
my records starts for row no 8 and column E its E8

I am not sure as what is the problem, any suggestions.

VBA Code:
Sub NokiaMDURawData()
   Dim Ary As Variant
   Dim i As Long
   'Dim i As String
   Dim Fnd As Range
   
   With Sheets("FTTX 050 Nokia MDU RawData")
      Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value2
   End With
   For i = 2 To UBound(Ary) -- ''[B]I am getting the error here[/B]
      Set Fnd = Range("8:8").Find(Ary(i, 5), , xlValues, xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then Fnd.EntireColumn.Delete
   Next i

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi vmjan02,

It is hard to say for sure because I don't know what your data looks like, but I think your array reference in the Find is the problem. Your array is filled with values from column A so the reference should be Ary(i) or maybe Ary(i,1). I don't use arrays often enough to remember the syntax very well.

Hope that helps,

Doug
 
Upvote 0
Hi Duggie33,

The column A and cell a2 to a18 has all the header names that need to be entire column, i am trying to loop all the headers
and my data startes for E8 and E8, F8 so on has the column headers.
 
Upvote 0
I am not following your layout, can you post some of your data with XL2BB? I still think your array reference is the issue. Have you stepped through the code and watched the Locals window to see how the array is referenced?

Doug
 
Upvote 0
I am sharing the screen shot, i am sure this will help, in this case A2 is the header name and column G needs to be deleted.
1693941536883.png
 
Upvote 0
If you don't have very many columns to delete, maybe less than 100 and you only need to do it one time, I would do this somewhat manually. In my example below, in row 7, MATCH is used to check if the heading listed in column A. If true, it was found in column A, and you can select the column and delete it.

Book1.xlsx
ABCDEFGHIJK
1Headers to Delete
2kpi22
3kpi2
4kpi
5
6
7FALSETRUEFALSETRUEFALSETRUEFALSE
8tps44kpi2tps66kpi22tps99kpikpi33
91234567
10567891011
119101112131415
1213141516171819
1317181920212223
1421222324252627
1525262728293031
1629303132333435
1733343536373839
1837383940414243
1941424344454647
2045464748495051
2149505152535455
2253545556575859
2357585960616263
2461626364656667
2565666768697071
2669707172737475
2773747576777879
Sheet1
Cell Formulas
RangeFormula
E7:K7E7=ISNUMBER(MATCH(E8,$A$2:$A$28,0))


If you want or need to do this in VBA, you need to straighten out your array syntax or approach it differently. I would create a range of the values in column A and then loop through the range with a For Each checking in row 8 with Find or Match.

Good luck,

Doug
 
Upvote 0
Maybe this:
VBA Code:
Sub NokiaMDURawData_2()
   Dim Ary As Variant, x As Variant
   Dim i As Long
   Dim Fnd As Range
   
   With Sheets("FTTX 050 Nokia MDU RawData")
      Ary = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value2
      If Not IsArray(Ary) Then Ary = Array(.Range("A2")) 'if there's only 1 value
   End With

   For Each x In Ary
      Set Fnd = Range("8:8").Find(What:=x, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, _
      SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
      If Not Fnd Is Nothing Then Fnd.EntireColumn.Delete
   Next

End Sub
 
Upvote 0
Solution
thanks guys the code is working perfect, you guys are gem
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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