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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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