Forum Discussion

ClaireMcF's avatar
ClaireMcF
Brass Contributor
Apr 03, 2024

VBA Code to Count Down Days

Hi All

 

I want to create a message box that pops when someone opens an excel file and it should read:

 

"Number of Days to Compliance: XXX"

 

I can create the message box in VBA but don't know how to write the code to make the number of days between two dates also populate in the box.  Can someone help please?

 

Thanks

7 Replies

  • ClaireMcF 

    Let's say the target date is in cell D2 on Sheet1.

    In the ThisWorkbook module:

     

    Private Sub Workbook_Open()
        Dim TargetDate As Date
        TargetDate = Worksheets("Sheet1").Range("D2").Value
        If TargetDate > Date Then
            MsgBox "Number of Days to Compliance: " & TargetDate - Date, vbInformation
        End If
    End Sub
    • ClaireMcF's avatar
      ClaireMcF
      Brass Contributor

      Thanks - you are a genius!
      How can I amend the above to make it count only working days from TODAY(), as each day refreshes please?

      • HansVogelaar's avatar
        HansVogelaar
        MVP

        ClaireMcF 

        Here is a version that uses the number non-weekend days:

        Private Sub Workbook_Open()
            Dim TargetDate As Date
            Dim NumberOfDays As Long
            TargetDate = Worksheets("Sheet1").Range("D2").Value
            NumberOfDays = Application.NetworkDays(Date, TargetDate) - 1
            If NumberOfDays > 0 Then
                MsgBox "Number of Days to Compliance: " & NumberOfDays, vbInformation
            End If
        End Sub

         

Resources