Im trying to check if a range of cells each have a value defined in another range
This is my current code:
Sub CheckInstallationName()
Dim LastRow As Long
With Worksheets(2)
LastRow = .Cells(.Rows.Count, Worksheets(1).Cells(4, 3).Value).End(xlUp).Row
End With
Dim rngA As Range
Set rngA = Range(Worksheets(1).Cells(4, 3).Value & "4:" & Worksheets(1).Cells(4, 3).Value & LastRow)
Dim cellA As Range
Dim InstallationNameRange As Variant
InstallationNameRange = Worksheets(1).Range("B16:B32").Value
For Each cellA In rngA
If UBound(Filter(InstallationNameRange, cellA.Value)) < 0 Then
'Some code
End If
Next cellA
End Sub
On the If UBound(filter(InstallationNameRange, cellA.Value)) < 0 Then
I get the error "Run-time error '13': Type mismatch" and cannot find a solution for this. Probably it is a very small fix. Without this if-statement the code works
Open a new Excel and write the following:
Public Sub CheckRangeInRange()
Dim rngA As Range
Dim rngB As Range
Dim rngCellA As Range
Dim rngCellB As Range
Dim blnError As Boolean
Set rngA = Worksheets(1).Range("A1:B10")
Set rngB = Worksheets(1).Range("D10:E20")
rngA.Interior.Color = vbYellow
rngB.Interior.Color = vbRed
For Each rngCellA In rngA
blnError = True
For Each rngCellB In rngB
If rngCellA = rngCellB Then
blnError = False
End If
Next rngCellB
If blnError Then Debug.Print "Display Error here!"
Next rngCellA
End Sub
Put some values in A1:B10
and D10:E20
and the addresses of the matching values would be printed in the immediate window.
You can't use Filter
on a 2-d range and any array created from a Range
is 2-d even if it is a single row or column.
You can use the 'double Transpose
trick` per this question . Note the highly up-voted answer, not the accepted one.
Eg:
Option Explicit
Sub Test()
Dim rng As Range
Set rng = Sheet2.Range("C20:E20") 'a, b, c
' use the double transpose trick to convert 2-d array to 1-d array
Dim arr As Variant
arr = Application.WorksheetFunction.Transpose( _
Application.WorksheetFunction.Transpose(rng.Value))
' now Filter will work
Dim out As Variant
out = Filter(arr, "a")
Debug.Print out(0)
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.