VBA Date range on IF statement, and count instances of appearance

muizac

Board Regular
Joined
Oct 24, 2006
Messages
50
Hi there,

Thanks for looking. I'm not bad at excel but visual basic is alien to me. I have a macro to extract a list of records if it's today's date. It works great. But rather than extracting only today's date, I'd like it to include the past 2 weeks as well (ie - date range From: minus 14 days To: today), plus append a column to count how many times a name appears in the extracted list (column C). The extract will look like this (column A is blank)

B1...................................C1...........D1
Monday, 27 March 2017......Andrew.....2
Tuesday, 28 March 2017......Andrew.....2
Tuesday, 28 March 2017......Barry........1

the Macro to ammend is....

Option Explicit

Sub FREQ()

Dim Today As Date
Dim LastRow As Long, I As Long
Dim J As Long
Application.EnableEvents = False ' TO AVOID TO LAUNCH SHEET EVENT MACRO
Today = Date
Range("B26:L" & Range("B" & Rows.Count).End(xlUp).Row + 1).ClearContents
With Sheets("Schedule")
LastRow = .Range("D" & Rows.Count).End(xlUp).Row
J = 2
For I = 2 To LastRow
If (.Cells(I, "D") = Today) Then
Cells(J, "B") = Today
Cells(J, "B").NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
Cells(J, "C") = .Cells(I, "C")

J = J + 1
End If
Next I
End With
Application.EnableEvents = True ' TO PERMIT TO LAUNCH SHEET EVENT MACRO
' Macro1 Macro
End Sub


Thank you so much for your help, it's very much appriciated.
Have a great day
Muizac
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this:
Code:
Sub FREQ()


    Dim Today As Date
    Dim LastRow As Long, I As Long
    Dim J As Long


    Application.EnableEvents = False ' TO AVOID TO LAUNCH SHEET EVENT MACRO


    Today = Date
    Range("B26:L" & Range("B" & Rows.Count).End(xlUp).Row + 1).ClearContents
    
    With Sheets("Schedule")
        LastRow = .Range("D" & Rows.Count).End(xlUp).Row
        J = 2
        For I = 2 To LastRow
            If (.Cells(I, "D") >= (Today - 14)) Then
                Cells(J, "B") = .Cells(I, "D")
                Cells(J, "B").NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
                Cells(J, "C") = .Cells(I, "C")
                Cells(J, "D").FormulaR1C1 = "=COUNTIF(C[-1],RC[-1])"
                J = J + 1
            End If
        Next I
    End With
    
Application.EnableEvents = True ' TO PERMIT TO LAUNCH SHEET EVENT MACRO


' Macro1 Macro
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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