VBA-Loop与一些工作表 [英] VBA-Loop with some worksheets

查看:147
本文介绍了VBA-Loop与一些工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,我想在我的excel文件的所有工作表中做一个循环,除了第一个。但是下面的代码只适用于第二个代码。可以请你解释一下这段代码有什么问题吗?



非常感谢

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆