【发布时间】:2017-03-22 23:49:05
【问题描述】:
我在Excel VBA: Loop through cells and copy values to another workbook 找到了一个直接应用于我正在尝试构建的代码的线程。
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim CurCell_1 As Range, CurCell_2 As Range
Dim Ran As Range
Dim Group As Range, Mat As Range
Application.ScreenUpdating = True
Set ws1 = ActiveWorkbook.Sheets("Scrap")
Set ws2 = ActiveWorkbook.Sheets("FC Detail")
For Each Mat In ws1.Range("E:E")
Set CurCell_2 = ws2.Range("F8")
For Each Group In ws1.Range("E:E")
Set CurCell_1 = ws1.Cells(Group.Row, Mat.Column)
If Not IsEmpty(CurCell_2) Then
CurCell_2.Value = CurCell_1.Value
End If
Next
Next
End Sub
这段代码有一个例外,它不断循环。
我认为If Not IsEmpty 将是 VBA 的描述符,一旦它到达列表末尾以停止程序。
【问题讨论】:
-
“不断循环”是什么意思?如果代码从不退出循环,你怎么知道它“有效”?
-
它一直在运行,因为您正在处理整个列...两次。为什么不找到最后一行然后限制你的范围,如另一个链接所示?