【问题标题】:Using Application.WorksheetFunction.Match returns type mismatch使用 Application.WorksheetFunction.Match 返回类型不匹配
【发布时间】:2021-06-06 09:19:27
【问题描述】:

我试图让 Application.WorksheetFunction.Match 在特定列的范围内的下一行中找到一个值,并将其与另一个工作表列中的值列表匹配,设置 a = 一个特定值,如果不返回错误。

我目前拥有的代码是:

Dim caseSearch As String
Dim caseCell As Range
Dim moveSearch As String
Dim moveCell As Range

caseSearch = "caseset"
moveSearch = "movementset"

Set caseCell = ws.Rows(1).Find(What:=caseSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

Set moveCell = ws.Rows(1).Find(What:=moveSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If caseCell Is Nothing Then
    MsgBox "caseset column not found"
ElseIf moveCell Is Nothing Then
    MsgBox "movementset column not found"
End If


Dim rng As Range
Dim row As Range
Dim cell As Range
Dim lRow As Long
Dim a As Integer
Dim b As Integer

lRow = Cells(Rows.Count, 1).End(xlUp).row

Set rng = ws.Range("A2:A" & lRow)

For Each row In rng.Rows
    If Not IsError(Application.WorksheetFunction.Match(row.Columns(caseCell).Value, tcc.Columns(1), 0)) Then
        a = tcc.Cells("C2")
    Else:
        a = tcc.Cells("C3")
    End If
   
Next row

【问题讨论】:

  • MATCH 中的第二个参数应该是地址(例如 A2:B4)。您正在传递一个列。
  • 好的,所以我修改为 tcc.Range("A2:A") 但我仍然收到类型不匹配错误
  • 我认为是第一个参数。 row.Columns(caseCell).Value 的值是多少? Columns() 调用需要一个索引,您提供的是一个范围。此外,您的 For Each 仅在单个列上运行,因此行无论如何都只是单个单元格。
  • 'row.Columns(caseCell).Value'的值是范围内的当前行,列号匹配caseCell的当前单元格值。如果这是有道理的。进一步向上 caseCell 设置为找到“caseset”作为标题的列。
  • 另见此答案:stackoverflow.com/questions/27302794/… Application.Match() 和 Application.WorksheetFunction.Match() 之间存在(有点奇怪)的区别。如果你想使用 IsError() 那么你需要使用 Application.Match()。

标签: excel vba worksheet-function


【解决方案1】:

匹配Application.Match

  • 这是一个“设置”示例。
Option Explicit

Sub UseApplicationMatch()

    Const caseSearch As String = "caseset"
    Const moveSearch As String = "movementset"
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
    
    Dim caseCell As Range
    Set caseCell = ws.Rows(1).Find(What:=caseSearch, LookIn:=xlValues, _
        LookAt:=xlWhole)
    If caseCell Is Nothing Then
        MsgBox "caseset column not found"
        Exit Sub
    End If
    Dim caseCol As Long: caseCol = caseCell.Column
    
    Dim moveCell As Range
    Set moveCell = ws.Rows(1).Find(What:=moveSearch, LookIn:=xlValues, _
        LookAt:=xlWhole)
    If moveCell Is Nothing Then
        MsgBox "movementset column not found"
        Exit Sub
    End If
    Dim moveCol As Long: moveCol = moveCell.Column
    
    Dim tcc As Worksheet: Set tcc = wb.Worksheets("Sheet2")
    Dim t1 As Long: t1 = tcc.Cells("C2").Value
    Dim t2 As Long: t2 = tcc.Cells("C3").Value
    
    Dim lRow As Long: lRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim rng As Range: Set rng = ws.Range("A2:A" & lRow)
    
    Dim fCell As Range
    Dim Result As Long
    For Each fCell In rng.Cells
        If IsNumeric(Application.Match(fCell.EntireRow.Columns(caseCol).Value, _
                tcc.Columns(1), 0)) Then
            Result = t1
        Else
            Result = t2
        End If
    Next fCell
    
End Sub

【讨论】:

    猜你喜欢
    • 2022-06-12
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多