Problem Cycling Through Worksheets VBA

carstenp

Board Regular
Joined
May 27, 2010
Messages
148
I'm pulling my hair out trying to figure out why this isn't working. I run the macro from Sheet2 and it just ends up clearing everything on that sheet rather than on the ws that's up in the rotation. It will work if I activate the ws first, but that doesn't seem like the best solution. Please help

Code:
Dim ws as Worksheet
Dim QueryCollection as Collection
Dim i as Integer, CIRows as Integer, CISectorColumn as Integer

'lots of prior code

   For Each ws In ThisWorkbook.Worksheets
        If (ws.Name = "A" Or ws.Name = "B" Or ws.Name = "C") Then
                
            With Sheet1
                Set QueryCollection = New Collection
                    For i = 6 To CIRows
                        If .Cells(i, CISectorColumn) = ws.Name Then
                            QueryCollection.Add (i)
                        End If
                    Next i
            End With
                
             ws.Activate      'Why do I need this line???
            
            With ws
                'Do a bunch of stuff
            End With
        End If
    Next ws
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Rich (BB code):
Dim i as Integer, CIRows as Integer, CISectorColumn as Integer

'lots of prior code
   For Each ws In ThisWorkbook.Worksheets
        If (ws.Name = "A" Or ws.Name = "B" Or ws.Name = "C") Then
             
            '*** The following process only does it on sheet1   
            With Sheet1
                Set QueryCollection = New Collection
                    For i = 6 To CIRows    '<---- This value is 0, so it does not enter this cycle.
                        If .Cells(i, CISectorColumn) = ws.Name Then
                            QueryCollection.Add (i)
                        End If
                    Next i
            End With
             '*** End Process
                
             ws.Activate      'Why do I need this line???  'If you work with the ws object, it is not necessary to select the sheet.           
            With ws
                'Do a bunch of stuff
            End With
        End If
    Next ws


Better explain what you need to do, what data you have and the final result.
 
Upvote 0
That entirely depends on the code located at


Turns out it was as simple as
Rich (BB code):
With ws​
Cells.Delete​
End With​
To
Rich (BB code):
With ws​
.Cells.Delete​
End With​

Crazy the amount of stress a missed period can have. Thanks for your help
 
Upvote 0
Glad you solved it & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top