循环查找值匹配条件会产生类型不匹配

时间:2015-10-28 20:38:27

标签: vba loops type-mismatch

我正在尝试遍历一个范围,查看该值是否与来自不同范围的另一个值匹配,如果是,它将输入匹配的值。循环工作一次,将匹配的值插入“E3”,然后产生“类型不匹配”错误。是否有善良的人可以建议为什么我的代码不起作用?我非常感激......

Sub find_that()
    ActiveWorkbook.Worksheets("Output").Activate 
    For Each a In Worksheets("Output").Range("E3:E28")
        For Each b In Worksheets("SelectionOfSKU").Range("F3:F66")
            If a.Offset(0, -3).Value = b.Value And b.Offset(0, 2) <> 0 Then
                a.Value = b.Offset(0, -4).Value 'If condition met, input the matched value
            End If
        Next b
    Next a
End Sub

1 个答案:

答案 0 :(得分:0)

pzle解决了,代码如下:

Sub find_sku2()

Dim a As Long, i As Long
Dim b As Long, j As Long

i = Sheets("Output").Range("E" & Rows.Count).End(xlUp).Row
j = Sheets("SelectionOfSKU").Range("F" & Rows.Count).End(xlUp).Row

For a = 3 To i
    For b = 1 To j
            If Sheets("Output").Cells(a, 2).Value = Sheets("SelectionOfSKU").Cells(b, 6).Value And Sheets("SelectionOfSKU").Cells(b, 8).Value <> 0 Then
                Sheets("Output").Cells(a, 5).Value = Sheets("SelectionOfSKU").Cells(b, 2).Value
            End If
        Next
Next

End Sub