aFTER THIS LINE OF CODE, i WANT TO ADD THE START OF THIS WEEKS DATE TO SHEET 'Booking'…
- User
aFTER THIS LINE OF CODE, i WANT TO ADD THE START OF THIS WEEKS DATE TO SHEET 'Booking' C38: MsgBox "Expired dates removed successfully", vbInformation, "EXPIRED DATES" End Sub
- Assistant
To add the start of this week's date to the
Bookingsheet at cellC38after displaying the message box, you first need to determine the start date of the current week (which can be considered as the previous Sunday or Monday depending on your weekly setup).Here’s how you can modify your code to achieve this in VBA:
MsgBox "Expired dates removed successfully", vbInformation, "EXPIRED DATES" ' Determine the start of the current week (Monday) Dim startOfWeek As Date startOfWeek = Date - Weekday(Date, vbMonday) + 1 ' Alternatively, to set start of the week as Sunday ' startOfWeek = Date - Weekday(Date, vbSunday) ' Add the start of this week's date to sheet 'Booking' at C38 Worksheets("Booking").Range("C38").Value = startOfWeekExplanation:
-
Determining the Start of the Week:
- returns the day of the week as an integer, with Monday as 1 and Sunday as 7. To find the start of the week (Monday), we subtract this value from the current date and then add to ensure we get the correct Monday date.
-
- User
can you please write me a VBA module that will do the following:
In sheet 'Booking' If the date in C38 is before the current week then Call Module7.DeleteExpiredDates
- Assistant
Certainly! Below is a simple VBA module that checks if the date in cell
C38of theBookingsheet is before the current week. If it is, it will call theDeleteExpiredDatesprocedure fromModule7.VBA Code
Sub CheckAndDeleteExpiredDates() Dim ws As Worksheet Dim dateToCheck As Date Dim startOfWeek As Date ' Set the worksheet variable Set ws = ThisWorkbook.Worksheets("Booking") ' Check if the date in C38 is a valid date If IsDate(ws.Range("C38").Value) Then dateToCheck = ws.Range("C38").Value ' Determine the start of the current week (Monday) startOfWeek = Date - Weekday(Date, vbMonday) + 1 ' Compare the date and call the delete procedure if needed If dateToCheck < startOfWeek Then Call Module7.DeleteExpiredDates End If Else MsgBox "The date in C38 is not valid.", vbExclamation, "Invalid Date" End If End Sub