user3540466
user3540466

Reputation: 303

Looping through all sheets

I am trying to loop through all the sheets within a workbook. I have the following code and it works totally fine; the only issue is that it only works if I launch the macro on the first sheet; if i do it in any other it just stops after the first loop.

 WS_Count = ActiveWorkbook.Worksheets.Count
    For i = 1 To WS_Count
        Dim ws1 As Worksheet
        Set ws1 = ThisWorkbook.ActiveSheet
        'if sheet contains evdre
        Set c = ws1.Cells.Find("blabla")
        If Not c Is Nothing Then
            'do things
        End If
     Next i

I am not sure if this issue is due to the fact that the macro should always be launched on the first sheet or if there is something wrong in the code. Thanks for any help

EDIT In the 'do sth part I am actually creating hidden sheets (hidden copy of the one that I am active at); can that make the count mess up? and thus only work in the first sheet

Upvotes: 1

Views: 1982

Answers (1)

iDevlop
iDevlop

Reputation: 25252

  Dim ws As Worksheet, c as range
  for each ws in ThisWorkbook.WorkSheets
  Set c = ws.Cells.Find("blabla")
  If Not c Is Nothing Then
        'do things
  End If

Upvotes: 3

Related Questions