When I enter a value in A6 of sheet Booking,
I am getting a Type Mismatch error on the line : selectedMonth = Month(monthValue) ' Extract month number
in the following VBA code.
' Check if the changed cell is A6
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, wsBooking.Range("A6")) Is Nothing Then
ThisWorkbook.Sheets("Booking").Unprotect Password:="edit"
Application.EnableEvents = False
ThisWorkbook.Sheets("Booking").Range("N2:Q40").ClearContents
Dim wsTargetAsset As Worksheet
Set wsTargetAsset = ThisWorkbook.Worksheets(wsBooking.Range("A2").Value)
' Clear previous formatting
'wsBooking.Range("D2:J36" & wsBooking.Cells(wsBooking.Rows.Count, "D").End(xlUp).Row).Interior.ColorIndex = xlNone
' Use Union to combine the specified ranges
Dim targetRangeA6 As Range
Set targetRangeA6 = Union(wsBooking.Range("D3:J8"), _
wsBooking.Range("D10:J15"), _
wsBooking.Range("D17:J22"), _
wsBooking.Range("D24:J29"), _
wsBooking.Range("D31:J36"))
' Set the interior color of the combined range to none
targetRangeA6.Interior.ColorIndex = xlNone
code
Assistant
User
This line of code was previously: For Each cell In wsBooking.Range("C2:C" & wsBooking.Cells(wsBooking.Rows.count, "C").End(xlUp).Row). I have cjhaged it to: For Each cell In wsBooking.Range("C2:C36" & wsBooking.Cells(wsBooking.Rows.count, "C").End(xlUp).Row). Do I have to change the last "C" at the end of the code?
Assistant
Yes, you need to change the last C at the end of the line. As it stands, the line you've modified attempts to construct a range with a concatenation of "C36" and the last used row in column C, which is incorrect and doesn't make sense in this context.
Correcting the Range
Your original line of code:
vba
For Each cell In wsBooking.Range("C2:C" & wsBooking.Cells(wsBooking.Rows.count, "C").End(xlUp).Row)
determines the range from C2 to the last non-empty cell in column .
' Get the selected value from A6
selectedValue3 = wsBooking.Range("A6").Value
' Find the column in 'WF Assets' corresponding to the selected value
Set foundColumn = wsTargetAsset.Rows(1).Find(What:=selectedValue3, LookIn:=xlValues, LookAt:=xlWhole)
If Not foundColumn Is Nothing Then
colIndex = foundColumn.Column
' Loop through each month-year in column C of 'Booking'
For Each cell In wsBooking.Range("C2:C" & wsBooking.Cells(wsBooking.Rows.count, "C").End(xlUp).Row)
monthValue = cell.Value ' This should be like "mmm yy"
selectedMonth = Month(monthValue) ' Extract month number
selectedYear = Year(monthValue) ' Extract year number
' Determine the cell range for the days of the month in the corresponding month (from D to J)
Set monthCol = wsBooking.Range(wsBooking.Cells(cell.Row, 4), wsBooking.Cells(cell.Row, 10)) ' D to J
' Loop through each day cell in the month range D to J
For Each dayCell In monthCol
' Check if the day cell is a date or numeric
If Not IsEmpty(dayCell.Value) And (IsNumeric(dayCell.Value) Or IsDate(dayCell.Value)) Then
' Convert the day cell value to a date if necessary
If Not IsDate(dayCell.Value) Then
dayDate = DateSerial(selectedYear, selectedMonth, CLng(dayCell.Value))
Else
dayDate = dayCell.Value
End If
dayValue = Day(dayDate) ' Extract just the day
' Compare with the found column in 'WF Assets'
lastRow3 = wsTargetAsset.Cells(wsTargetAsset.Rows.count, colIndex).End(xlUp).Row
For startRow2 = 2 To lastRow3
' Get the asset date
Dim assetsDate As Date
assetsDate = wsTargetAsset.Cells(startRow2, colIndex).Value
' Checking both day, month, and year
If Day(assetsDate) = dayValue And Month(assetsDate) = selectedMonth And Year(assetsDate) = selectedYear Then
' Highlight the matching date in the Booking sheet
dayCell.Interior.Color = RGB(255, 0, 0) ' Red Color
End If
Next startRow2
End If
Next dayCell
Next cell
Else
MsgBox "Selected item not found in 'WF Assets' headers."
End If
wsBooking.Range("A8").ClearContents
wsBooking.Range("A10").ClearContents
wsBooking.Range("A12").ClearContents
wsBooking.Range("A14:A25").ClearContents
wsBooking.Range("A27").ClearContents
Application.EnableEvents = True
ThisWorkbook.Sheets("Booking").Protect Password:="edit"
Call Module6.LoadLiveBookings
End If
The Type Mismatch error you're encountering typically occurs in VBA when you try to use a value of one data type where a different type is expected. In your case, the error happens at the line where you extract the month from monthValue:
vba
selectedMonth = Month(monthValue) ' Extract month number
Possible Causes
Incorrect Data Format: The value in monthValue is likely not in a valid date format for the Month function to process it correctly. If cell.Value does not represent a valid date, attempting to extract the month will cause a type mismatch.
Text Format: If the value in cell C2:C35 is formatted as text that looks like a date but isn't recognized by VBA as a date, you'll encounter this issue.
Solutions
Check the Value Type: Before trying to extract the month, ensure that monthValue is indeed a date. You can do this with the IsDate function:
vba
If Not IsDate(monthValue) Then
MsgBox "The value '" & monthValue & "' in cell " & cell.Address & " is not a valid date."
Exit For
End If
Adding this check will help you identify when the issue occurs and prevent the error from interrupting your code.
Convert Text to Date: You can manually convert the string representing the date to a valid date using DateValue. Before extracting the month, you can do this:
vba
If IsDate(monthValue) Then
selectedMonth = Month(DateValue(monthValue)) ' Extract month number
Else
MsgBox "The value '" & monthValue & "' in cell " & cell.Address & " is not a valid date."
Exit For
End If
Ensure the Correct Format in Excel: Make sure that the cells in column C actually contain values in a date format. You could format them as dates in Excel to avoid issues.
Revised Part of Your Code
Here is how you can implement these suggestions:
vba
' Loop through each month-year in column C of 'Booking'
For Each cell In wsBooking.Range("C2:C" & wsBooking.Cells(wsBooking.Rows.Count, "C").End(xlUp).Row)
monthValue = cell.Value ' This should be like "mmm yy"
' Check if monthValue is a valid date
If IsDate(monthValue) Then
selectedMonth = Month(DateValue(monthValue)) ' Extract month number
selectedYear = Year(DateValue(monthValue)) ' Extract year number
Else
MsgBox "The value '" & monthValue & "' in cell " & cell.Address & " is not a valid date."
Exit For
End If
' Proceed with the rest of your logic...
Conclusion
Utilizing the IsDate function will help you safeguard against potential errors related to invalid date formats, allowing your code to run more smoothly and making it easier to debug in the future. Ensure your data types are consistent throughout your code to minimize the chances of encountering similar issues.
C
If you're changing it to target a fixed range of C2:C36, you should specify that directly as follows:
vba
For Each cell In wsBooking.Range("C2:C36")
Final Code Adjustment
Here's the corrected loop:
vba
' Loop through each month-year in the fixed range C2:C36
For Each cell In wsBooking.Range("C2:C36")
monthValue = cell.Value ' This should be like "mmm yy"
' Check if monthValue is a valid date
If IsDate(monthValue) Then
selectedMonth = Month(DateValue(monthValue)) ' Extract month number
selectedYear = Year(DateValue(monthValue)) ' Extract year number
Else
MsgBox "The value '" & monthValue & "' in cell " & cell.Address & " is not a valid date."
Exit For
End If
' Proceed with the rest of your logic...
Conclusion
By directly referring to the fixed range C2:C36, you sidestep any issues with dynamic row counts, ensuring the loop processes only the desired cells. This also eliminates any possibility of concatenation mistakes which can lead to runtime errors. Make sure to review any other parts of the code that rely on the count or values from column C to ensure they align with your new fixed range.