使用两个索引/匹配时,运行时错误1004无法获取匹配属性

时间:2018-06-13 22:36:12

标签: vba excel-vba excel

尝试使下面的代码有效,但不幸的是第二个索引/匹配会引发错误。如果我从第二部分删除“count”变量(在第一个索引/匹配中正常工作),并且引用单个单元格,不知道为什么,代码可以工作。

尝试启动一个新的计数器变量,第2部分仍然会抛出错误。另外,有没有更好的方法来引用一个范围内的单元格,在for循环中,而不是我使用的贫民窟解决方案?

谢谢!

Dim sht As Worksheet
Dim LastRow As Long
Dim count As Integer

Set sht = ActiveSheet
LastRow = sht.Cells(sht.Rows.count, "A").End(xlUp).Row

count = 2

    For Each i In Range("f2:f" & LastRow)
        With Application.WorksheetFunction
            i.Value = .Index(Worksheets("Area").Range("c:c"), .Match(Range("E" & count), Worksheets("Area").Range("a:a")))
        End With
        count = count + 1
    Next

count = 2

    For Each i In Range("h2:h" & LastRow)
    i.Value = count
        With Application.WorksheetFunction
            i.Value = .Index(Worksheets("Park reason").Range("C:C"), .Match(Range("G" & count), Worksheets("Park reason").Range("A:A")))
        End With
        count = count + 1
    Next

End Sub

2 个答案:

答案 0 :(得分:1)

很可能找不到比赛。测试未找到的匹配项。如果您使用Application.Match,则可以在尝试获取i.value之前使用测试中返回的错误来查看是否找到了匹配项。对两次匹配尝试都执行相同的操作。

With Application.WorksheetFunction
    Dim test As Variant
    test = Application.Match(Range("E" & count), Worksheets("Area").Range("a:a"), 0)
    If Not IsError(test) Then
        i.Value = .Index(Worksheets("Area").Range("c:c"), test)
    End If
End With

我可能会重写为:

With sht
    Dim test As Variant
    test = Application.Match(.Range("E" & count), Worksheets("Area").Range("A:A"), 0)
    If Not IsError(test) Then
        i.Value = Application.WorksheetFunction.Index(Worksheets("Area").Range("C:C"), test)
    End If
End With

我还希望使用比整列更小的范围,即不是" C:C"例如。找到使用过的范围/最后一行,然后再进行操作。

富勒版:

Option Explicit
Sub test()

    Dim sht As Worksheet
    Dim LastRow As Long
    Dim count As Long
    Set sht = ActiveSheet
    LastRow = sht.Cells(sht.Rows.count, "A").End(xlUp).Row

    With sht
        count = 2
        Dim i As Range, test As Variant
        For Each i In .Range("F2:F" & LastRow)
            test = Application.Match(.Range("E" & count), Worksheets("Area").Range("A:A"), 0)
            If Not IsError(test) Then
                i.Value = Application.WorksheetFunction.Index(Worksheets("Area").Range("C:C"), test)
            End If
            count = count + 1
        Next
        count = 2
        Dim test2 As Variant
        For Each i In .Range("H2:H" & LastRow)
            test2 = Application.Match(.Range("G" & count), Worksheets("Park reason").Range("A:A"))
            If Not IsError(test2) Then
                i.Value = Application.WorksheetFunction.Index(Worksheets("Park reason").Range("C:C"), test2)
            End If
            count = count + 1
        Next
    End With
End Sub

答案 1 :(得分:0)

完全代码顺便说一句,不知道为什么我在复制到清理子时遇到了重复错误,但是解决了它的工作原理。

Sub Cleanup()

Dim sToday As String
sToday = Format(Date, "mm-dd-yyyy")

ActiveSheet.Copy Before:=Worksheets("Park Reason")
Rows("1:12").Select
Selection.Delete Shift:=xlUp
Range("G:H").Select
Selection.UnMerge
Columns("H:H").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Range("A:I").AutoFilter Field:=6, Criteria1:="Yes"
Columns("G:G").EntireColumn.AutoFit
Range("A:I").AutoFilter Field:=7, Criteria1:=Array("1" _
    , "11", "12", "13", "14", "15", "16", "30", "40", "50", "51", "99"), Operator:= _
    xlFilterValues
Range("A:I").AutoFilter Field:=1, Criteria1:=Array( _
    *removed*), Operator:=xlFilterValues
Range("A:I").RemoveDuplicates Columns:=2, Header:= _
    xlYes

For Each i In Range(Range("G2"), Range("G2").End(xlDown))
    If IsNumeric(i) Then
        i.Value = CDec(i.Value)
    End If
Next

For Each i In Range(Range("B2"), Range("B2").End(xlDown))
     If IsNumeric(i) Then
        i.Value = CDec(i.Value)
    End If
Next

Columns("F:F").Select
Selection.Delete ' Shift:=xlToLeft
Selection.Insert ' Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("F1").Value = "Area"
Columns("H:H").Insert
Range("H1").Value = "Parking Reason"
ActiveSheet.Name = sToday

test

End Sub

Sub test()

Dim sht As Worksheet
Dim LastRow As Long
Dim count As Long
Set sht = ActiveSheet
LastRow = sht.Cells(sht.Rows.count, "A").End(xlUp).Row

With sht
    count = 2
    Dim i As Range, test As Variant
    For Each i In .Range("F2:F" & LastRow)
        test = Application.Match(.Range("E" & count), Worksheets("Area").Range("A:A"), 0)
        If Not IsError(test) Then
            i.Value = Application.WorksheetFunction.Index(Worksheets("Area").Range("C:C"), test)
        End If
        count = count + 1
    Next
    count = 2
    Dim test2 As Variant
    For Each i In .Range("H2:H" & LastRow)
        test2 = Application.Match(.Range("G" & count), Worksheets("Park reason").Range("A:A"))
        If Not IsError(test2) Then
            i.Value = Application.WorksheetFunction.Index(Worksheets("Park reason").Range("C:C"), test2)
        End If
        count = count + 1
    Next
End With
End Sub