I am getting a Type Mismatch error on this line; If TimeValue(currentTime) >=…
- User
I am getting a Type Mismatch error on this line; If TimeValue(currentTime) >= TimeValue(timePeriods(rangeIndex)) And TimeValue(currentTime) < TimeValue(timePeriods(rangeIndex + 1)) Then In the following event:
Sub ShowTime() Dim currentTime As Date currentTime = Now Dim ranges(27) As Range Dim timePeriods(10) As Variant Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Daily") Set ranges(1) = ws.Range("C5:D6") Set ranges(2) = ws.Range("C11:D12") Set ranges(3) = ws.Range("C17:D18") Set ranges(4) = ws.Range("E5:F6") Set ranges(5) = ws.Range("E11:F12") Set ranges(6) = ws.Range("E17:F18") Set ranges(7) = ws.Range("G5:H6") Set ranges(8) = ws.Range("G11:H12") Set ranges(9) = ws.Range("G17:H18") Set ranges(10) = ws.Range("I5:J6") Set ranges(11) = ws.Range("I11:J12") Set ranges(12) = ws.Range("I17:J18") Set ranges(13) = ws.Range("K5:L6") Set ranges(14) = ws.Range("K11:L12") Set ranges(15) = ws.Range("K17:L18") Set ranges(16) = ws.Range("M5:N6") Set ranges(17) = ws.Range("M11:N12") Set ranges(18) = ws.Range("M17:N18") Set ranges(19) = ws.Range("O5:P6") Set ranges(20) = ws.Range("O11:P12") Set ranges(21) = ws.Range("O17:P18") Set ranges(22) = ws.Range("Q5:R6") Set ranges(23) = ws.Range("Q11:R12") Set ranges(24) = ws.Range("Q17:R18") Set ranges(25) = ws.Range("S5:T6") Set ranges(26) = ws.Range("S11:T12") Set ranges(27) = ws.Range("S17:T18") timePeriods(1) = "06:59:59" ' 6:59am - before any time period" timePeriods(2) = "08:00:00" ' 8am" timePeriods(3) = "09:00:00" ' 9am" timePeriods(4) = "10:00:00" ' 10am" timePeriods(5) = "11:00:00" ' 11am" timePeriods(6) = "12:00:00" ' 12pm" timePeriods(7) = "13:00:00" ' 1pm" timePeriods(8) = "14:00:00" ' 2pm" timePeriods(9) = "15:00:00" ' 3pm" timePeriods(10) = "16:00:00" ' 4pm - after all time periods" Dim i As Integer For i = 1 To 27 Dim rangeIndex As Integer If i Mod 3 = 1 Then ' Determine the time period based on the index of the range rangeIndex = (i + 2) \ 3 Else rangeIndex = ((i + 2) \ 3) - 1 End If If TimeValue(currentTime) >= TimeValue(timePeriods(rangeIndex)) And TimeValue(currentTime) < TimeValue(timePeriods(rangeIndex + 1)) Then ranges(i).Interior.Color = RGB(255, 255, 0) ' Change the background color to Yellow End If Next i End Sub
- Assistant
The Type Mismatch error occurs because you are trying to compare a Date value with a Variant value (timePeriods). To fix this error, you need to declare the timePeriods array as a Date array instead of a Variant array.
Change this line:
Dim timePeriods(10) As Variant
To:
Dim timePeriods(10) As Date
This will ensure that the elements of the timePeriods array are treated as Date values, allowing you to compare them with the currentTime value correctly.
- User
I want a VBA code that will clear all the cell background colours in the following ranges. A5:U6 A11:U12 A17:U18
- Assistant
You can use the following VBA code to clear the cell background colors in the specified ranges:
Sub ClearCellBackgroundColors() Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Daily") With ws.Range("A5:U6") .Interior.Color = xlNone End With With ws.Range("A11:U12") .Interior.Color = xlNone End With With ws.Range("A17:U18") .Interior.Color = xlNone End With End SubMake sure to replace "Daily" with the actual name of the worksheet where you want to clear the cell background colors.
This code uses the
Rangeobject and theInterior.Colorproperty to remove the background color from the specified ranges. The.Interior.Color = xlNonestatement sets the background color to "No Fill", effectively clearing any existing color.