Excel VBA - 宏运行但无效

时间:2017-02-16 21:17:42

标签: excel vba excel-vba

我是Excel VBA的新手,这是我的第一个宏,所以如果我犯了一个非常明显的错误,请原谅我。我有以下代码与工作表进行比较,如果找到匹配项,则在其中一个工作表上做笔记。它运行时没有错误,但没有进行更改。我看不出哪里出错了。在此先感谢您的帮助。

Sub invalid()

Dim i As Integer
Dim j As Integer

Dim main As Worksheet
Dim invalid As Worksheet

i = 2
Set main = ThisWorkbook.Worksheets(1)
Set invalid = ThisWorkbook.Worksheets(2)

Do
    j = 2
    Do
        If LCase$(invalid.Cells(i, 1).Value) = LCase$(main.Cells(j, 13).Value) Then
            main.Cells(j, 14).Value = "Invalid Email"
        End If
        j = j + 1
    Loop While main.Cells(j, 2) = Not Null
    i = i + 1
Loop While invalid.Cells(i, 2) = Not Null


End Sub

1 个答案:

答案 0 :(得分:1)

试试这个,它会删除其中一个循环:

Sub invalid()

Dim i As Long
Dim j As Long
Dim lRow As Long
Dim main As Worksheet
Dim invalid As Worksheet

Set main = ThisWorkbook.Worksheets(1)
Set invalid = ThisWorkbook.Worksheets(2)
lRow = main.Cells(main.Rows.Count, 13).End(xlUp).Row

For i = 2 To lRow
    j = 0
    On Error Resume Next
    j = Application.WorksheetFunction.Match(main.Cells(i, 13), invalid.Range("A:A"), 0)
    On Error GoTo 0
    If j > 0 Then main.Cells(i, 14) = "Invalid Email"
Next i

End Sub