当列中的值为空时macros不会停止

我使用下面的代码填充我的主表(在代码中称为sht)的一列。 在这张表(sht)中,当从C85下来时,我想让macros在C列的空单元格中停止。 我将如何去追求这个目标?

Sub VTZ() Dim sht As Worksheet, sht2 As Worksheet Set sht = Sheets("16-Compliancy-Rebuild") Set sht2 = Sheets("OpmerkingBackup") Dim Table1 As Range Dim Table2 As Range Dim Dept_Row As Long Dim Dept_Clm As Long Dim rng As Range Set Table1 = sht.Range("D85:D1185") Set Table2 = sht2.Range("B3:B1103") Dept_Row = sht.Range("H85").Row Dept_Clm = sht.Range("H85").Column For Each cl In Table1 Set rng = Table2.Find(cl, SearchDirection:=xlPrevious, LookAt:=xlWhole) If Not rng Is Nothing Then sht.Cells(Dept_Row, Dept_Clm) = sht2.Cells(rng.Row, 6).Value End If Dept_Row = Dept_Row + 1 Next cl MsgBox "Done" End Sub 

尝试了很多谷歌正手,似乎无法find正确的方法来做到这一点。

尝试更新这个部分…

 For Each cl In Table1.cells Set rng = Table2.Find(cl, SearchDirection:=xlPrevious, LookAt:=xlWhole) If Not rng Is Nothing Then sht.Cells(Dept_Row, Dept_Clm) = sht2.Cells(rng.Row, 6).Value else Exit For 'exits the loop End If Dept_Row = Dept_Row + 1 Next cl 

我不确定这是否是您想要退出循环的正确部分。 另一种方式就是…

 For Each cl In Table1.cells If Isempty(cl) then Exit Sub 'this completely exits the macro Endif Set rng = Table2.Find(cl, SearchDirection:=xlPrevious, LookAt:=xlWhole) If Not rng Is Nothing Then sht.Cells(Dept_Row, Dept_Clm) = sht2.Cells(rng.Row, 6).Value End If Dept_Row = Dept_Row + 1 Next cl