VBA-Loop with some sheets

I am new and I would like to loop through all sheets of my excel file except the first one. However, the code below only works on the second. Could you explain to me what is wrong with 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 
+4
source share
2 answers

If you want to skip the first sheet, change the loop as shown below. Worksheets(i + 1) will give you errors if there are only 40 sheets in your book;)

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) Using On Error Resume Next is evil;) Use it only when necessary.

2) Do not use .SELECT It slows down your code. Instead, do the action directly. for instance

 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 

NTN

Sid

+3
source

You must change:

 Worksheets(1 + 1).Select 

so it uses your i variable ... you just put 1 + 1 so that it always evaluates to 2

Classic mistake :)

+1
source

Source: https://habr.com/ru/post/1403569/


All Articles