VBA-Loop与一些工作表 [英] VBA-Loop with some worksheets
问题描述
非常感谢
Sub MobileTCalculation()
'MobileTCalculation宏
Dim i As Integer
对于i = 1至40
工作表(1 + 1)。选择
范围(A20)。选择
错误恢复下一步
下一步i
End Sub
如果要跳过第一个工作表,然后更改循环,如下所示。 工作表(i + 1)
将给您错误,如果您的工作簿中只有40页;)
使用这个
Sub MobileTCalculation()
Dim i As Integer
For i = 2 To 40
工作表(i).Range(A20)。选择
Next i
End Sub
还有两件事。
1)使用 On Error Resume Next
是邪恶的;)仅使用它
2)不要使用 .SELECT
减慢你的码。而是直接执行动作。例如
Sub MobileTCalculation()
Dim i As Integer
For i = 2到40
与工作表(i).Range(A20)
Debug.Print .Value
结束
下一步i
End Sub
HTH
Sid
I am a beginner and I would like to do a loop in all the worksheets of my excel file, except the first one. However the code below only works on the second one. Could you please explain me what is wrong in this code?
Many thanks
Sub MobileTCalculation()
'MobileTCalculation Macro
Dim i As Integer
For i = 1 To 40
Worksheets(1 + 1).Select
Range("A20").Select
On Error Resume Next
Next i
End Sub
If you want to skip the first sheet then change the loop as shown below. Worksheets(i + 1)
will give you errors if there are only 40 sheets in your workbook ;)
Use this
Sub MobileTCalculation()
Dim i As Integer
For i = 2 To 40
Worksheets(i).Range("A20").Select
Next i
End Sub
Also two things.
1) Use of On Error Resume Next
is evil ;) Use it only when necessary.
2) Don't use .SELECT
It slows down your code. Instead directly perform the action. For example
Sub MobileTCalculation()
Dim i As Integer
For i = 2 To 40
With Worksheets(i).Range("A20")
Debug.Print .Value
End With
Next i
End Sub
HTH
Sid
这篇关于VBA-Loop与一些工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!