当 Column 中的值为空时宏不会停止
Macro won't stop when value in Column is empty
我正在使用下面的代码填充主 Sheet 中的一列(在代码中称为 sht)。在此 sheet (sht) 中,我希望宏在从 C85 下来时遇到 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
尝试了很多关于正手的谷歌搜索,但似乎找不到正确的方法。
尝试更新这部分...
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
我正在使用下面的代码填充主 Sheet 中的一列(在代码中称为 sht)。在此 sheet (sht) 中,我希望宏在从 C85 下来时遇到 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
尝试了很多关于正手的谷歌搜索,但似乎找不到正确的方法。
尝试更新这部分...
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