我有一张包含多张纸的工作簿,我正在尝试检测C9中的数字:C200并将它们复制到一个单元格的三列中 - 对于从第7页开始的每张纸。最外面的forloop中的代码在单个工作表上工作正常,但是当我尝试将其循环通过其余工作表时,我收到错误消息。我想我有两个问题:
我不确定如何在工作簿中循环播放c9:c200范围内的c9:c200范围
我想我需要告诉VBA重置评估,以便在下一张纸上从头开始。
我是否正朝着正确的方向前进?
谢谢!
Sub MoveQtrLoop()
Dim CEL As Range
Dim RNG As Range
Dim I As Integer
Dim WrkSht As Worksheet
Dim WS_Count As Integer
I = 0
WS_Count = ActiveWorkbook.Worksheets.Count
For Each WrkSht In ActiveWorkbook.Worksheets
I = I + 1
If 6 < I < WS_Count Then
Set RNG = ActiveSheet.Range("c9:C200")
For Each CEL In RNG
If CEL.HasFormula = True Then
CEL.Offset(, 3) = CEL.Value
ElseIf IsNumeric(CEL) = True Then
CEL.Offset(, 3) = CEL.Value
Else
End If ****ERR. OCCURS HERE****
Next CEL
Else
End If
Next WrkSht
End Sub
答案 0 :(得分:1)
当您声明范围时,您正在基于ActiveSheet执行此操作,但您没有通过每个循环激活以下工作表。但是,不要使用ActiveSheet,只需使用您的WrkSht变量设置目标范围:
Set RNG = WrkSht.Range("c9:C200")
希望这可以解决您的问题。
答案 1 :(得分:0)
它没有循环,因为您正在使用ActiveSheet
的范围。将其更改为:
Dim CEL As Range
Dim RNG As Range
Dim I As Integer
Dim WrkSht As Worksheet
Dim WS_Count As Integer
I = 0
WS_Count = ActiveWorkbook.Worksheets.Count
For Each WrkSht In ActiveWorkbook.Worksheets
I = I + 1
If 6 < I < WS_Count Then
Set RNG = wrksht.Range("c9:C200")
For Each CEL In RNG
If CEL.HasFormula = True Then
CEL.Offset(, 3) = CEL.Value
ElseIf IsNumeric(CEL) = True Then
CEL.Offset(, 3) = CEL.Value
Else
End If
Next CEL
Else
End If
Next WrkSht
End Sub