Help Converting Formula to VBA: Fill cell range with date of Fridays this month

Batteredburrito

New Member
Joined
Jul 31, 2018
Messages
15
Hi, I have the current formula which when pasted into Cell Q8 and Dragged down to Q12, populates the cells with the date of each friday this month:

Code:
<code>=IFERROR(AGGREGATE(15,6,ROW(INDEX(A:A,EOMONTH(TODAY(),-1)+1):INDEX(A:A,EOMONTH(TODAY(),0)))/(WEEKDAY(ROW(INDEX(A:A,EOMONTH(TODAY(),-1)+1):INDEX(A:A,EOMONTH(TODAY(),0))),1)=6),ROW(1:1)),"-")</code>

Im trying to convert this into VBA as i understand VBA more than formulas. However was wondering if anyone here could possibly help.

It really is appreciated
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi,

Try to use the following code:

Code:
Option Explicit


Sub Fridays()
    Dim lngMonth            As Long
    Dim lngYear             As Long
    Dim lngWeekday          As Long
    Dim lngDate             As Long
    Dim i                   As Integer
    
    'Today's date values:
[COLOR=#ff0000]    lngDate = DateSerial(2018, 7, 1)[/COLOR]
    lngMonth = Month(lngDate)
    lngYear = Year(lngDate)
    lngWeekday = WorksheetFunction.Weekday(DateSerial(lngYear, lngMonth, 1), 2)
    
    With Sheet1
        'Return the first Friday of the current month
        If lngWeekday <= 5 Then
                .Range("B3").Value = DateSerial(lngYear, lngMonth, 1 + (5 - lngWeekday))
            Else
                .Range("B3").Value = DateSerial(lngYear, lngMonth, 1 + 12 - lngWeekday)
        End If
        
        'Return the remaining dates
        For i = 1 To 5
            If Month(.Range("B3").Value + i * 7) = lngMonth Then
                .Range("B" & i + 3).Value = .Range("B3").Value + i * 7
            Else
                Exit Sub
            End If
        Next i
        
    End With


End Sub

It will return all Friday dates for a given month in Sheet1, cell range B3:B8. You can use the highlighted line to change the date values (year-month-day, although day is always ignored), or simply replace it with:
lngDate = Date
...to always use today's date.

I hope it helps.

Best regards,
Justyna
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,800
Members
449,468
Latest member
AGreen17

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