Loop through workbook and apply module to each monthly sheet

TheJonWithNoH

New Member
Joined
Sep 8, 2017
Messages
30
I have the following code set up to pull data from a raw data file which searches through a list of dates (column R) and returns the data from column C to monthly sheets (Jan, Feb, etc). In cells F1 and H1 on each of these monthly sheets is the first and last date of each month, respectively. How can I modify the code below to not just provide January's data ("Jan" sheet) but to loop through the entire year and return the data for those months?

Code:
Sub LoopRange()Application.ScreenUpdating = False


Dim iWS As Worksheet: Set iWS = ThisWorkbook.Sheets("Data")
Dim oWS As Worksheet: Set oWS = ThisWorkbook.Sheets("Jan")


Dim inputRange As Range: Set inputRange = iWS.Range("R5", iWS.Range("R5").End(xlDown))
Dim outputRange As Range: Set outputRange = oWS.Range("A28:A200")


Dim startDate As Date: startDate = oWS.Range("F1").Value
Dim endDate As Date: endDate = oWS.Range("H1").Value


Dim cRow As Long: cRow = 28
Dim iCell As Range, oCell As Range




For Each iCell In inputRange
    If iCell.Value >= startDate And iCell.Value <= endDate Then
        cRow = cRow + 1
        oWS.Range("A" & cRow).Value = iCell.Offset(0, -15).Value
    End If
Next iCell


ActiveSheet.Range("A29:A200").RemoveDuplicates Columns:=1, Header:=xlNo


Application.ScreenUpdating = True
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Are there any sheets to be Excluded from the loop ?
 
Upvote 0
UNTESTED

Code:
Sub LoopRange()
Application.ScreenUpdating = False
Dim iWS As Worksheet: Set iWS = Sheets("Data")
Dim inputRange As Range: Set inputRange = iWS.Range("R5", iWS.Range("R5").End(xlDown))
Dim cRow As Long: cRow = 28
Dim iCell As Range, oCell As Range
Dim ws As Worksheet
Dim startDate As Date: startDate = ws.Range("F1").Value
Dim endDate As Date: endDate = ws.Range("H1").Value
Dim outputRange As Range: Set outputRange = ws.Range("A28:A200")
    For Each ws In Worksheets
        If ws.Name <> "Data" And ws.Name <> "Setup" Then
        ws.Activate
            For Each iCell In inputRange
                If iCell.Value >= startDate And iCell.Value <= endDate Then
                    cRow = cRow + 1
                    ws.Range("A" & cRow).Value = iCell.Offset(0, -15).Value
                End If
            Next iCell
        ActiveSheet.Range("A29:A200").RemoveDuplicates Columns:=1, Header:=xlNo
        End If
    Next ws
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you...

I get a "Run-time error '91': Object Variable or With block variable not set "

Debug highlights the ": startDate = ws.Range("F1").Value"
 
Upvote 0
Still UNTESTED

Code:
Sub LoopRange()
Application.ScreenUpdating = False
Dim iWS As Worksheet: Set iWS = Sheets("Data")
Dim inputRange As Range: Set inputRange = iWS.Range("R5", iWS.Range("R5").End(xlDown))
Dim cRow As Long: cRow = 28
Dim iCell As Range, oCell As Range
Dim ws As Worksheet
Dim startDate As Date: startDate = Range("F1").Value
Dim endDate As Date: endDate = Range("H1").Value
Dim outputRange As Range: Set outputRange = Range("A28:A200")
    For Each ws In Worksheets
        If ws.Name <> "Data" And ws.Name <> "Setup" Then
        ws.Activate
            For Each iCell In inputRange
                If iCell.Value >= startDate And iCell.Value <= endDate Then
                    cRow = cRow + 1
                    ws.Range("A" & cRow).Value = iCell.Offset(0, -15).Value
                End If
            Next iCell
        ActiveSheet.Range("A29:A200").RemoveDuplicates Columns:=1, Header:=xlNo
        End If
    Next ws
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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