2017-01-04 100 views
0

我正在嘗試編寫一個宏,該宏檢查一張表中名爲原始數據的每個行,以查找匹配的名稱,如果名稱匹配,則將該行中的數據複製到表單稱爲名稱搜索。我正在嘗試使用do while while循環來做到這一點,這與我過去成功使用的類似。Excel vba儘管循環不識別do while語句

但是,當我嘗試運行它時,它會給我錯誤「Loop without do」,儘管事實上一切似乎都在正確的位置。我的代碼如下:

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 

我不能爲我的生活弄清楚我做錯了什麼。任何幫助改善我的代碼將不勝感激!

+2

你需要另一端IF – Rdster

+0

IM盲目謝謝你這麼密歇根州 – Vbasic4now

回答

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 
+1

確定。我看到Rdster打敗了我。 – rohrl77