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: Private Sub Worksheet_Activate() Call CopyPA ' Call the CopyPA code Application.Calculate ' Perform calculations after the data is copied End Sub
Sub CopyPA() Dim today As String today = Format(Now, "ddd") ' Get the current day ' Define the source and destination ranges Dim sourceRange As Range Dim destRange As Range Set sourceRange = Worksheets("PA").Range("A3:P3") ' Source range in the "PA" sheet ' Destination range in the "Daily" sheet ' Adjust the row number (3) according to your requirements Set destRange = Worksheets("Daily").Range("D3:S3") ' Clear the destination range and copy the format from the source range destRange.ClearContents sourceRange.Copy destRange.PasteSpecial Paste:=xlPasteFormats ' Copy the data from the source range to the destination range based on the current day Select Case today Case "Mon" sourceRange.Copy destRange Case "Tue" sourceRange.Offset(1).Copy destRange Case "Wed" sourceRange.Offset(2).Copy destRange Case "Thu" sourceRange.Offset(3).Copy destRange Case "Fri" sourceRange.Offset(4).Copy destRange End Select Application.CutCopyMode = False ' Clear the copy mode Call CopyAA End Sub
Sub CopyAA() Dim today As String today = Format(Now, "ddd") Dim sourceRange As Range Dim destRange As Range Set sourceRange = Worksheets("AA").Range("A3:L3") ' Source range in the "PA" sheet Set destRange = Worksheets("Daily").Range("A9:L9") destRange.ClearContents sourceRange.Copy destRange.PasteSpecial Paste:=xlPasteFormats Select Case today Case "Mon" sourceRange.Copy destRange Case "Tue" sourceRange.Offset(1).Copy destRange Case "Wed" sourceRange.Offset(2).Copy destRange Case "Thu" sourceRange.Offset(3).Copy destRange Case "Fri" sourceRange.Offset(4).Copy destRange End Select Application.CutCopyMode = False Call CopyCL End Sub
Sub CopyCL() Dim today As String today = Format(Now, "ddd") Dim sourceRange As Range Dim destRange As Range Set sourceRange = Worksheets("CL").Range("A3:O3") ' Source range in the "CL" sheet Set destRange = Worksheets("Daily").Range("F15:T15") destRange.ClearContents sourceRange.Copy destRange.PasteSpecial Paste:=xlPasteFormats Select Case today Case "Mon" sourceRange.Copy destRange Case "Tue" sourceRange.Offset(1).Copy destRange Case "Wed" sourceRange.Offset(2).Copy destRange Case "Thu" sourceRange.Offset(3).Copy destRange Case "Fri" sourceRange.Offset(4).Copy destRange End Select Application.CutCopyMode = False Call ShowTime End Sub
Sub ShowTime() Dim currentTime As Date currentTime = Now ' Get the current time ' Define the ranges and their corresponding time periods Dim ranges(27) As Range Dim timePeriods(10) As String ' Assign the worksheet and ranges Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Daily") ' Replace "Daily" with your sheet name 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"
- Assistant
The type mismatch error is likely occurring because the
timePeriodsarray is declared as a string array, but you are trying to compare its values with the current time using theTimeValuefunction, which returns a date/time value.To fix this error, you need to modify the
timePeriodsarray to be declared as aVariantarray instead of aStringarray. This will allow it to store date/time values. Here's the modified code:Sub ShowTime() Dim currentTime As Date currentTime = Now ' Get the current time ' Define the ranges and their corresponding time periods Dim ranges(27) As Range Dim timePeriods(10) As Variant ' Change from String array to Variant array ' Assign the worksheet and ranges Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Daily") ' Replace "Daily" with your sheet name ' Rest of the code remains the same... ' ... End SubBy making this change, the
TimeValuefunction will be able to correctly convert the elements of thetimePeriodsarray into date/time values for comparison with thecurrentTime.