Excel vba while while循环无法识别do while语句

时间:2017-01-04 15:39:04

标签: excel vba excel-vba while-loop

我正在尝试编写一个宏来检查一个名为原始数据的表中的每一行是否匹配名称,如果名称匹配,则将该行中的数据复制到名为“名称搜索”的表单中。我试图使用类似于我过去成功使用的do while循环来执行此操作。

然而,当我尝试运行它时,它会给出错误"循环没有做"尽管事实似乎一切都在正确的地方。我的代码如下:

Sub NameSearch()

Sheets("Raw Data").Unprotect ("29745")
Application.ScreenUpdating = False

Dim x As Long

'set starting point at row 2
x = 2

Dim sourceSheet As Worksheet: Set sourceSheet = ThisWorkbook.Worksheets("Raw Data")
Dim destSheet As Worksheet: Set destSheet = ThisWorkbook.Worksheets("Name Search")

Do While sourceSheet.range("A" & x).Value <> ""

    If sourceSheet.range("O" & x).Value <> destSheet.range("B2") Then
        x = x + 1

    Else
    If sourceSheet.range("O" & x).Value = destSheet.range("B2") Then

'selects the next row where the 1st column is empty
            lMaxRows = destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Row
'pastes the data from the specified cells into the next empty row
            destSheet.range("A" & lMaxRows + 1).Value = sourceSheet.range("A" & x).Value
            destSheet.range("B" & lMaxRows + 1).Value = sourceSheet.range("B" & x).Value
            destSheet.range("C" & lMaxRows + 1).Value = sourceSheet.range("C" & x).Value
            destSheet.range("D" & lMaxRows + 1).Value = sourceSheet.range("D" & x).Value
            destSheet.range("E" & lMaxRows + 1).Value = sourceSheet.range("E" & x).Value
            destSheet.range("F" & lMaxRows + 1).Value = sourceSheet.range("F" & x).Value
            destSheet.range("G" & lMaxRows + 1).Value = sourceSheet.range("G" & x).Value
            destSheet.range("H" & lMaxRows + 1).Value = sourceSheet.range("F" & x).Value - sourceSheet.range("G" & x).Value
            destSheet.range("I" & lMaxRows + 1).Value = sourceSheet.range("M" & x).Value
            destSheet.range("J" & lMaxRows + 1).Value = sourceSheet.range("N" & x).Value

        x = x + 1

    End If

Loop

End Sub

我不能为我的生活弄清楚我做错了什么。任何帮助改进我的代码将不胜感激!

1 个答案:

答案 0 :(得分:1)

您错过了End If ...见下文

Sub NameSearch()

Sheets("Raw Data").Unprotect ("29745")
Application.ScreenUpdating = False

Dim x As Long

'set starting point at row 2
x = 2

Dim sourceSheet As Worksheet: Set sourceSheet = ThisWorkbook.Worksheets("Raw Data")
Dim destSheet As Worksheet: Set destSheet = ThisWorkbook.Worksheets("Name Search")

Do While sourceSheet.range("A" & x).Value <> ""

    If sourceSheet.range("O" & x).Value <> destSheet.range("B2") Then
        x = x + 1

    Else
    If sourceSheet.range("O" & x).Value = destSheet.range("B2") Then

'selects the next row where the 1st column is empty
            lMaxRows = destSheet.Cells(destSheet.Rows.Count, "A").End(xlUp).Row
'pastes the data from the specified cells into the next empty row
            destSheet.range("A" & lMaxRows + 1).Value = sourceSheet.range("A" & x).Value
            destSheet.range("B" & lMaxRows + 1).Value = sourceSheet.range("B" & x).Value
            destSheet.range("C" & lMaxRows + 1).Value = sourceSheet.range("C" & x).Value
            destSheet.range("D" & lMaxRows + 1).Value = sourceSheet.range("D" & x).Value
            destSheet.range("E" & lMaxRows + 1).Value = sourceSheet.range("E" & x).Value
            destSheet.range("F" & lMaxRows + 1).Value = sourceSheet.range("F" & x).Value
            destSheet.range("G" & lMaxRows + 1).Value = sourceSheet.range("G" & x).Value
            destSheet.range("H" & lMaxRows + 1).Value = sourceSheet.range("F" & x).Value - sourceSheet.range("G" & x).Value
            destSheet.range("I" & lMaxRows + 1).Value = sourceSheet.range("M" & x).Value
            destSheet.range("J" & lMaxRows + 1).Value = sourceSheet.range("N" & x).Value

        x = x + 1

    End If
End If '<----MISSING END IF
Loop

End Sub