Trying To Avoid Cells With A Time Value

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code below that steps through all the cells of a range and applies code depending on the cell's content.
In one case, if the cell contains a time value, ignore it and move on. (code highlighted as blue)

Rich (BB code):
                    Set rngpda = ws9.Range("H13:Q" & rwpdaed)
                    For Each cell In rngpda
                        ccnt = ccnt + 1
                        cval2 = cell.Value
                        Debug.Print cell.Address & " : " & cval2
                        If cval2 = "" Then
                            Debug.Print "Service ignored: Empty"
                        ElseIf Right(cval2, 3) = " AM" Or Right(cval2, 3) = " PM" Then
                            Debug.Print "Pre-service ignored: " & cval2
                        ElseIf IsDate(Format(Now(), "yyyy-mm-dd ") & cval2) = True Then
                            Debug.Print "Service ignored (time): " & cval2
                        ElseIf cval2 = "NA" Or Right(cval2, 3) = "NR" Then
                            Debug.Print "Service ignored: " & cval2
                        ElseIf cval2 = "AUTO" Or cval2 = "USER" Then
                            Debug.Print "Service ignored: " & cval2
                        Else
                            cntcval2 = Application.WorksheetFunction.CountIf(ws_staff.Columns(4), cval2)
                            If cntcval2 > 0 Then
                                Debug.Print "Match: " & cval2 & " [" & l & "]"
                            Else
                                'Stop
                                MsgBox cval2 & " at " & cell.Address & " is not in the roster." & Chr(13) & "Please adjust the name to an employee that is working", vbCritical, "Error: Staff mismatch"
                                cell.Font.Color = vbRed
                                frm_sniroster.Show
                                tsnr = tsnr + cntrpl
                            End If
                        End If
                    Next cell

Here is a sample data in which I'm encountering problems:

WS 28-Jun-22.xlsx
Q
138:30P
14
159:30P
169:30P
178:00P
188:00P
RPL


When the code assessing the cells reaches Q17 and Q18, the code in blue does not catch it and the routine continues to be caught at the else statement. It shouldn't get that far as those cells have "time" values in them (?). Interestingly, unlike cells Q15, Q15 and Q16 where the time values are text, Q17 and Q18 are numeric. I would have thought if I were going to have problems it would be with the text values.

Anyone have any suggestions on how to overcome this? If any cell is or looks like a time (in the format "h:mmA/P" that cell should be ignored.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
When asking for help with code it is better to show all the code because the problem might not be where you think it is. In this case I need to see how you are declaring variable cval2.

You are converting the data to text before you test it for being date/time data, so the result will always be FALSE. Change your code to this:

VBA Code:
                        ElseIf IsDate(cval2) Then

However, you should really test the cell directly:

VBA Code:
                        ElseIf IsDate(cell) Then

(BTW you don't have to compare a Boolean function to True, because it will already be either True or False.)
 
Upvote 0
Hello Jeff!
'cval2' is a variable declared publicly as string in another module. I have a lot of modules including the one where that is declared that to post would have been a lot. With so much code I'm hesitate to post pages of what I think might be redundant.

I replaced...
Code:
ElseIf IsDate(Format(Now(), "yyyy-mm-dd ") & cval2) = True Then
with your suggestion ...
Code:
ElseIf IsDate(cell) Then

And the (true) time value in that cell isn't caught. If the value in cell = 8:00 PM, the user gets the msgbox pop up saying:
"0333333333333333 at $Q$17 is not in the roster."

Hope this helps?
 
Upvote 0
It is not a good idea to be mixing time "value" and time as "text" in the same column.
Given what you are trying to do and that you have cval2 as String, if you change cval2 = cell.Value to
Rich (BB code):
cval2 = cell.Text
You should then be able to drop the ElseIf IsDate line.

For Context Only:
VBA Code:
                    For Each cell In rngpda
                        ccnt = ccnt + 1
                        'cval2 = cell.Value
                        cval2 = cell.Text
 
Upvote 0
Solution
Thank you Alex, I will implement this next time I have conflicting data to test it on. I think it will be rare to have the contents of the cells as numeric values. I think the problem lies in the data source that provides the value to this worksheet. That data source might be fed by different sources using different data entry methods. I think your solution will ensure that data is always text.
 
Upvote 0
Sounds good. Let us know how you go.

By the way cell.text is not converting what is in the cell to text but it reads the cell as displayed .
So the cell might have the time as 0.333333333333333 being numeric value of the time in the cell, the cell is formatted to display 08:00 AM, then value and value2 will return 0.333333333333333 while cell.text will return 08:00 AM.
 
Last edited:
Upvote 0
Hi Alex. Excel sure is powerful when one realizes how different functions work within. When I first started using excel, I saw it simply as a place to make calculations on a grid. Then I discovered basic VBA, and with each lesson I get taught here by the good folk of Mr. Excel, I'm gain8ing even greater respect. Thank you.

I did make the change as suggested, but found I still needed to include
Code:
ElseIf IsDate(Format(Now(), "yyyy-mm-dd ") & cval2)
in order to bypass anything that could be a time. (whether a text representation or a true value) . Perhaps I did something wrong that would require it as you had indicated it shouldn't be needed anymore.

Happy to say, I'm getting the results I'm looking for so far in my testing.
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,766
Members
449,336
Latest member
p17tootie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top