您的代码似乎有很多异常情况。我会在下面稍微重构一下
Dim wsS As Worksheet
Dim wsU As Worksheet
Dim i As Integer
Set wsS = Sheets("sheet1") 'this has assigned the worksheet sheet1 to wsS
Set wsU = Sheets("non_confid") 'this has assigned the worksheet sheet1 to wsU
col1 = "A"
col2 = "E"
For i = 505 To 700
If Not IsEmpty(wsS.Range(col1 & i).Value) Then
wsU.Range(col2 & i - 503).Value = wsS.Range(col1 & i).Value
End If
Next
它的行为类似于您的代码,但应该正确复制。我不确定您是否希望它只是跳过空值或停止执行。要跳过范围内的空值,以下版本的代码将起作用。
Dim wsS As Worksheet
Dim wsU As Worksheet
Dim i As Integer
Dim j As Integer
Set wsS = Sheets("sheet1") 'this has assigned the worksheet sheet1 to wsS
Set wsU = Sheets("non_confid") 'this has assigned the worksheet sheet1 to wsU
col1 = "A"
col2 = "E"
j = 2
For i = 505 To 700
If Not IsEmpty(wsS.Range(col1 & i).Value) Then
wsU.Range(col2 & j).Value = wsS.Range(col1 & i).Value
j = j + 1 'here is a new counter for the position on the second page.
End If
Next
如果您希望循环在找到一个空单元格时停止,那么下面的代码应该可以工作。
Dim wsS As Worksheet
Dim wsU As Worksheet
Dim i As Integer
Dim j As Integer
Set wsS = Sheets("sheet1") 'this has assigned the worksheet sheet1 to wsS
Set wsU = Sheets("non_confid") 'this has assigned the worksheet sheet1 to wsU
col1 = "A"
col2 = "E"
j = 2
For i = 505 To 700
If IsEmpty(wsS.Range(col1 & i).Value) Then
Exit For 'this jumps out of the loop, so no more copying.
Else
wsU.Range(col2 & j).Value = wsS.Range(col1 & i).Value
j = j + 1
End If
Next