Hi
Chris
At fist glance, everything makes perfect sense to me, and I don’t find anything strange or confusing in what is happening.
( I am not saying that makes me clever. Quite the opposite: - more often than not, I miss the point totally, so get the right answer , by mistake … if you understand my meaning … a problem I often have is that I am too stupid to understand the logic of more intelligent people getting it wrong…. ( The best Experts are correct when they are ( very rarely ) wrong.. , I can’t understand that. They can. )
_.____________
So:-
This not returning any
Debug.Print output at your
Case points is exactly as I would expect
Code: Select all
Dim strDT1 As String
strDT1 = "02/17/2020 10:42:21 AM"
Dim strDT2 As String
strDT2 = "02/17/2020 10:39:53 AM"
Dim dt As Date
dt = DateValue(strDT1) + TimeValue(strDT1) ' Date/Time for this scheduled event
Dim dtNow As Date
dtNow = DateValue(strDT2) + TimeValue(strDT2) ' use a constant date for the select statement
Debug.Print Format(dt, "yyyy/mm/dd hh:mm:ss"), Format(dtNow, "yyyy/mm/dd hh:mm:ss")
Select Case dt
Case (dt = 0) ' Nothing to do with this record; ignore it completely
Debug.Print "Nothing to do"
Case (dt <= dtNow) ' This record is past due; run it now
Debug.Print "RUNning "
Case (dt > dtNow) ' This record is immature; retain it as a Remainder
Debug.Print "Postponed"
End Select
As I see it, with or without the extra
( ) the
Select Case point and also the 3
Case points are going to evaluate what you wrote there, at these 4 points:-
___ dt
(dt = 0)
(dt <= dtNow)
(dt > dtNow)
So, at those 4 points VBA returns
True,
False , or something like
17.02.2020 10:42:21
___ dt ‘ returns something like
17.02.2020 10:42:21
(dt = 0) ‘ returns False
(dt <= dtNow) ‘ returns False
(dt > dtNow)’ returns True
( For all I know, it might even do some of that evaluating at compiling or at the start of the macro even before it gets there. I am not sure)
In any case, ( or in any
Select Case :) ) , your coding actually looks like this
Code: Select all
Select Case dt
Case False ' Nothing to do with this record; ignore it completely
Debug.Print "Nothing to do"
Case False ' This record is past due; run it now
Debug.Print "RUNning "
Case True
Debug.Print "Postponed"
Or , more actually, it is doing this
Code: Select all
Select Case 17.02.2020 10:42:21
Case False ' Nothing to do with this record; ignore it completely
Debug.Print "Nothing to do"
Case False ' This record is past due; run it now
Debug.Print "RUNning "
Case True
Debug.Print "Postponed"
In that last code snippet, you are trying to find a match to
17.02.2020 10:42:21. But you are only giving it the choices of
False,
False, and
True,
If dt were a Boolean and set to
True, then the
Case True would be selected. ( Debug.Print "Postponed" ).
So Hans idea to get your thing to work makes sense.
Either of these would do the same
Code: Select all
Dim Boo As Boolean: Let Boo = True
Select Case Boo
Case (dt = 0) ' Nothing to do with this record; ignore it completely
Debug.Print "Nothing to do"
Case (dt <= dtNow) ' This record is past due; run it now
Debug.Print "RUNning "
Case (dt > dtNow) ' This record is immature; retain it as a Remainder
Debug.Print "Postponed"
End Select
Code: Select all
Select Case True
Case (dt = 0) ' Nothing to do with this record; ignore it completely
Debug.Print "Nothing to do"
Case (dt <= dtNow) ' This record is past due; run it now
Debug.Print "RUNning "
Case (dt > dtNow) ' This record is immature; retain it as a Remainder
Debug.Print "Postponed"
End Select
In my naive way of thinking about it, you are trying to match what is placed after the
Select Case with what is after the
Cases.
Its as simple as that.
_._____
In many situation in VBA : if you give it a value, it takes it; If you give it a variable it takes the value in it; If you give it a
x>y type thingy , then it takes the
True or
False evaluation.
So in your case…
If you put a value in, it takes that
If you put a variable in, it takes the value of it
If you put a logical expression in, then it takes the result of that ,
True or
False
_.______________
A few other examples might help get across what I am trying to say…
Code: Select all
Sub SelectNutCases()
Dim Nut1 As Long, Nut2 As String, Nut3 As Double
Let Nut1 = 1: Nut2 = "2": Nut3 = 3
Select Case 3
Case Nut1
Debug.Print "Won't see this"
Case Nut2
Debug.Print "Won't see this"
Case Nut3
Debug.Print "You'll see this"
End Select
End Sub
It is just a case of matching stuff.
Your original macro was trying to match a date time string value to either
Fasle or
False or
True
So you had no match found.
_._________________
ChrisGreaves wrote:…., not one of the CASE statements evaluates to True.
That is not quite right
The third
Case point evaluated to True
ThirdCaseIstTue.JPG : https://imgur.com/5pVbruu" onclick="window.open(this.href);return false;
ThirdCaseIstTrue.JPG
But in that Third
Case bit you were you were trying to match
True to something like
17.02.2020 10:42:21 – that is not the matched case
In Hans modification you are trying at that third Case point to match
True to the
True which Hans put at the
Select Case point – that is the matched case.
_.______________
As I understand it ,the VBA Select Case statement thing, works such as to pick the case that matches.
You could try to match a
False , if you like
Code: Select all
Sub SelectFalse()
Select Case False
Case 1 = 1 ' is True
Debug.Print "Won't see this"
Case UCase("tWo") = "TWO" ' is True
Debug.Print "Won't see this"
Case "two" = "TWO" ' is False
Debug.Print "You'll see this"
End Select
End Sub
_._________________________
Alan
_._____
P.s.
HansV wrote:..... It may have been from rory...
possible, I noticed he sometimes knows Excel stuff quite good.
You do not have the required permissions to view the files attached to this post.
I am having difficulty logging in with this account just now.
You can find me at DocAElstein also