For Loop or variable help

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
The following code finds all the Fridays within a date range and then will bold that date. Within my workbook I have 6 sheets with the tabs labeled sheet1(Chart), 2015, 2016...2019. My worksheet is layed out with the date is in column A on all the sheets labeled with a year, on sheet2 the date range starts in 'A2' with the value of 1/1/2015 all the way to 'A366' with the value of '12/31/2015'(1 additional cell is added for the years that contain leap years). The year will change to match the sheet tab name.
The problem I am having is when I start with spreadsheet tab '2015 it will work, the value of c holds the values of the cell dates. I am aware that c is declared as range and not a date. When the For Loop goes to the next sheet and activates it and the loop begins for that sheet, the value of c still loops but contains sheet 2015 values and not 2016.
I setup Watches with Expressions
ActiveSheet.Name, c, and I. Both ActiveSheet.Name and I work as expected, ActiveSheet.Name displays the next sheets name and I increments by 1 but the value of c is the problem. What am I doing wrong? Thank You

Code:
Option Explicit

Sub PullFridays1()
    Dim dat As Range
    Dim c As Range
    Dim rw As Integer
    Dim WS_Count As Integer
    Dim I As Integer
    
    Set dat = ActiveSheet.Range("A2:A367")


    WS_Count = ActiveWorkbook.Worksheets.Count
    
    For I = 2 To WS_Count
    ThisWorkbook.Worksheets(I).Activate
        For Each c In dat
            If Weekday(c) = vbFriday Then
                Debug.Print c.Value
                c.Font.Bold = True
            End If
        Next c
    Next I
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Code:
Option Explicit

Sub PullFridays1()[COLOR=#d3d3d3]
    Dim dat As Range
    Dim c As Range
    Dim rw As Integer
    Dim WS_Count As Integer
    Dim I As Integer
[/COLOR]    
    Set dat = ActiveSheet.Range("A2:A367")


    [COLOR=#d3d3d3]WS_Count = ActiveWorkbook.Worksheets.Count
    
    For I = 2 To WS_Count
    ThisWorkbook.Worksheets(I).Activate
        For Each c In dat
            If Weekday(c) = vbFriday Then
                Debug.Print c.Value
                c.Font.Bold = True
            End If
        Next c
    Next I[/COLOR]
End Sub

You have set your range object variable to the active sheet so your For Each c in dat loop is only referring to that range

Try this update & see if resolves


Rich (BB code):
Sub PullFridays1()
    Dim dat As Range, c As Range
    Dim WS_Count As Integer, I As Integer
    


    WS_Count = ActiveWorkbook.Worksheets.Count
    
    For I = 2 To WS_Count
    Set dat = ThisWorkbook.Worksheets(I).Range("A2:A367")
        For Each c In dat
            If Weekday(c) = vbFriday Then
                Debug.Print c.Value
                c.Font.Bold = True
            End If
        Next c
        Set dat = Nothing
    Next I
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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