0

Issue with "Loop through all the sheets" with VBA

I would like to check on all sheets that is there a cell in the 6nd row which contains "text". The code is the following but it does not work. It works only on the active sheet.

        For Each ws In ThisWorkbook.Worksheets
            
            For j = 1 To 30
                If Cells(6, j) = "text" Then
                MsgBox (j)
                Else
                End If
            Next j
        
        Next ws

If I just for example: range("A1")="Yes" it works on all sheet.

braX
  • 11,506
  • 5
  • 20
  • 33
FoFE
  • 45
  • 4
  • 4
    `ws.cells(6, j)` – Warcupine May 23 '23 at 13:51
  • If you don't specify the parent worksheet for `Cells`, it assumes `ActiveSheet` is its parent. Which means it wont change as `ws` iterates through the `Worksheets` collection. As Warcupine pointed out, you need to use `ws` as the parent object for `Cells` so that it changes sheets with the loop. `ws.Cells(6, j)`. – Toddleson May 23 '23 at 13:55

0 Answers0