VBA检查是否在另一张纸中找到一张纸中的值

时间:2019-09-04 07:56:20

标签: excel vba function

我想开发一个宏,以检查工作表中一列的值是否在另一工作表的列中作为子字符串找到。

因此,我想检查第一列的每个单元格,如果不为空,请将其与第二张表的第1列和第3列的每个单元格进行比较。

为此,我使用了“ for each”循环,然后使用了“ for”循环,i从1到Rows.Count。

这是棘手的问题,因为我不确定这是否是正确的方法。另外,用于检查是否以子字符串形式找到值的instr()函数的参数似乎不匹配,因为在尝试运行代码时出现“类型不匹配”错误。

Sub test()

Set Wks1 = Worksheets("Sheet1")
Set Wks2 = Worksheets("Sheet2")

Dim i As Long
Dim rng As Range, cell As Range

Set rng = Wks1.Range("C3:O69")

For Each cell In rng

    If Not (IsEmpty(cell.Value)) Then

        For i = 1 To Wks2.Rows.Count

            If (InStr(Cells(i, 1), cell.Value, 1) <> 0) Or (InStr(Cells(i, 3), _
                                                            cell.Value, 1) <> 0) Then
                Cells(i, 4) = "String contains substring"

            End If

        Next i

    End If

Next cell

End Sub

1 个答案:

答案 0 :(得分:2)

应该可以

{{1}}
相关问题