looping through worksheet & call a subroutine

KNKN9

Board Regular
Joined
Mar 27, 2017
Messages
92
Hi,

I have the code
Code:
 Dim ws As WorksheetFor Each ws In Workbooks("iteration_status.xlsm").Worksheets
If ws.Name <> "dd" And ws.Name <> "Summary" Then
Call CCIT6
End If
Next
End Sub

where CCIT6 is
Code:
Range("B7").End(xlToRight).AutoFilter Field:=4, Criteria1:="IT6"Range(Range(Range("B8"), Range("B8").Offset(0, 1)), Range(Range("B8"), Range("B8").Offset(0, 1)).End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Copy
Sheet1.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Range(Range("B8").End(xlToRight), Range("B8").End(xlToRight).End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Copy
Sheet1.Cells(Rows.Count, "G").End(xlUp).Offset(1, 0).PasteSpecial


However the code only works for the first worksheet. Can someone please help me spot where I am going wrong.

Many thanks!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You only refer to one sheet, Sheet1, in the code for CCIT6.

Which sheet(s) should the rest of the code refer to ?
 
Upvote 0
Sheet(1) needs to be fixed as the is the sheet that information get pasted into..... I need for every sheet bar "Summary" and "dd" hence the code
Code:
[COLOR=#333333]or Each ws In Workbooks("iteration_status.xlsm").Worksheets
[/COLOR][COLOR=#333333]If ws.Name <> "dd" And ws.Name <> "Summary" Then[/COLOR]
 
Upvote 0
Replace above with following and try:
Rich (BB code):
Sub LoopSheets()

    Dim x   As Long
    Const KeepSheet As String = "dd|Summary"
    
    Application.ScreenUpdating = False
    
    For x = 1 To Worksheets.Count
        With Sheets(x)
            If InStr(KeepSheet, .Name) = 0 Then Call CCIT6(Sheets(x))
        End With
    Next x
    
    Application.ScreenUpdating = True
     
End Sub


Sub CCIT6(ByRef wks As Worksheet)
    
    Dim LC  As Long
    Dim LR  As Long
    
    Application.ScreenUpdating = False
    
    With wks
        LC = Application.Max(2, .Cells(7, .Columns.Count).End(xlToLeft).Column)
        LR = Application.Max(8, .Cells(.Rows.Count, 2).End(xlUp).Row)
        
        With .Cells(7, 2).Resize(LR - 6, LC - 1)
            .AutoFilter field:=4, Criteria1:="IT6"
            .Offset(1).Resize(LR - 7, 2).SpecialCells(xlCellTypeVisible).Copy
            Sheet1.Cells(Rows.Count, 5).End(xlUp).Offset(1).PasteSpecial xlPasteValues
            .Offset(1).Resize(LR - 7).SpecialCells(xlCellTypeVisible).Copy
            Sheet1.Cells(Rows.Count, 7).End(xlUp).Offset(1).PasteSpecial xlPasteValues
        End With
    End With
    
    Application.CutCopyMode = False
        
End Sub
 
Last edited:
Upvote 0
You need to explicitly select (or refer to in CCIT6) each worksheet in turn - simplest is to insert ws.Select
as follows:

Code:
 Dim ws As Worksheet
For Each ws In Workbooks("iteration_status.xlsm").Worksheets
If ws.Name <> "dd" And ws.Name <> "Summary" Then
ws.Select
Call CCIT6
End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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