How to compare two dates in text boxes? - vba

I want to compare two dates in text boxes.
Public Function CourseStatus(ByVal RefDate2 As Variant) As String
Dim Description As String
If Len(RefDate2) > 0 And IsDate(RefDate2) Then
Select Case DateDiff("d", Date, RefDate2)
Case Is > 60
CourseStatus = "In Date"
Case Is > 0
CourseStatus = "Expiring"
Case Is = [ParticipationDate]
CourseStatus = "Not Refreshed"
Case Else
CourseStatus = "Expired"
End Select
Else
CourseStatus = "Please Book"
End If
End Function
If [ParticipationDate] & [RefDate2] match return "Not Refreshed" as CourseStatus.
I need to do this before running the rest of the code to give "in Date" "Expiring" "Expired" and if none of this applies display "Please Book".
e.g
ParticipationDate 1/1/19
RefDate2 1/1/19
CourseStatus "Not Refreshed"

Sorry I didn't read your code super accurately.
Your datediff function returns a integer value not a date. You should use
Case is 0
to check for a date match.

You might use something like this:
If Len(RefDate2) > 0 And IsDate(RefDate2) Then
Select Case DateDiff("d", Date, RefDate2)
Case Is > 60
CourseStatus = "In Date"
Case Is > 0
CourseStatus = "Expiring"
Case Else
If DateDiff("d", RefDate2, [ParticipationDate]) = 0 Then
CourseStatus = "Not Refreshed"
Else
CourseStatus = "Expired"
End If
End Select
Else
CourseStatus = "Please Book"
End If

Related

VBA Select case 1 to 100 only taking 1

I'm trying to make a select case that identifies if a number is lower than 0, 1 to 100 or greater than 100, the thing is that is just doesn't work. Here's my code:
If IsNumeric(TxtTemp.Text) Then
Select Case TxtTemp.Text
Case Is <= 0
TxtEstado.Text = "Solid"
Case 1 To 100
TxtEstado.Text = "Liquid"
Case Is > 100
TxtEstado.Text = "Gas"
End Select
Else
TxtEstado.Text = ""
End If
I know that this is an easy thing to do, the thing is that the select case returns liquid only if the number received is equal to 1. If it is lower or equal to 0 it returns solid, but if it is equal or greater to 2, it returns gas. I don't understand what I'm doing wrong.
Maybe it is easier to use a function for this kind of conversion
Function chText(txt As String) As String
On Error GoTo EH
Dim resTxt As String
If IsNumeric(txt) Then
Select Case CDbl(txt)
Case Is <= 0
resTxt = "Solid"
Case 1 To 100
resTxt = "Liquid"
Case Is > 100
resTxt = "Gas"
End Select
Else
resTxt = ""
End If
chText = resTxt
Exit Function
EH:
chText = "Error"
End Function
Sub Tester()
Debug.Print chText("101")
' TxtEstado.Text = chText(TxtTemp.Text)
End Sub

Select Case statement and data validation

I am trying to validate data ranges using the select case statement. I am having issues with the other nested select cases. Is this possible or am i wishfull thinking? Or should i separate the select case statements to be stacked?
For instance this is my code in vb:
Select Case intyear
Case 2000 To 2025
Select Case intmonth
Case 1 To 12
BlnDateValid = True
End Select
Select Case intDay
Case 1 To 31
BlnDateValid = True
End Select
Select Case intHours
Case 0 To 23
BlnDateValid = True
End Select
Select Case intAddDays
Case 0 To 60
BlnDateValid = True
End Select
Select Case intAddHours
Case 0 To 23
BlnDateValid = True
End Select
Case Else
BlnDateValid = False
End Select
If blnDatevalid = false then
MessagebBox.Show("Please check all fields and enter valid
data", "Invalid data", MessageBoxButtons.OK)
Unfortunately, the indenting making sense doesn't help the code make sense. The whole point of Select Case is to neatly select one of multiple cases. A Select Case with one case is bad code and you should be using an If statement instead. You should especially be using a If statement in this case because you can replace all those Select Case statements with a single If statement.
If Not (intyear >= 2000 AndAlso intyear <= 2025 AndAlso
intmonth >= 1 AndAlso intmonth <= 12 AndAlso
intDay >= 1 AndAlso intDay <= 31 AndAlso
intHours >= 0 AndAlso intHours <= 23 AndAlso
intAddDays >= 0 AndAlso intAddDays <= 60 AndAlso
intAddHours >= 0 AndAlso intAddHours <= 23) Then
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If
All the Select or If checks will still leave you vulnerable to non-sensical values like February 30. Better to actually attempt to create a DateTime value.
Dim d As DateTime
Dim t As TimeSpan
Try
d = New DateTime(intYear, intMonth, intDay, intHours, 0, 0)
t = New TimeSpan(intAddDays, intAddHours, 0, 0)
If t > (New TimeSpan(60, 23, 0, 0)) Then Throw New ArgumentOutOfRangeException()
Catch
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End Try
or you can create a string and try parsing it:
If Not DateTime.TryParse($"{intYear}-{intMonth}-{intDay} {intHours}:00:00")
MessagebBox.Show("Please check all fields and enter valid data", "Invalid data", MessageBoxButtons.OK)
End If

Is Null return ""

I have the following code however if the RefDate is blank it is returning #Type!. Can I add in a line to say ifNull = ""
Public Function Status(ByVal RefDate As Date) As String
Dim Description As String
Select Case DateDiff("d", Date, RefDate)
Case > 60
Description = "In date"
Case > 0
Description = "Expiring"
Case Else
Description = "Expired"
End Select
Status = Description
End Function
If Null return "" nothing or blank
Try this, I've changed the input data type also
Public Function Status(ByVal RefDate As Variant) As String
Dim Description As String
If Len(RefDate) > 0 and IsDate(RefDate) Then
Select Case DateDiff("d", Date, RefDate)
Case Is > 60
Status = "In date"
Case Is > 0
Status = "Expiring"
Case Else
Status = "Expired"
End Select
Else
Status = "No date"
End If
End Function
Trying a similar approach, when comparing with #Ryan Wildry, but now explicitly setting "" to Description
Public Function Status(ByVal RefDate As Date) As String
Dim Description As String
If CInt(RefDate) = 0 Then
Description = ""
Else
Select Case DateDiff("d", Date, RefDate)
Case Is > 60
Description = "In date"
Case Is > 0
Description = "Expiring"
Case Else
Description = "Expired"
End Select
End If
Status = Description
End Function

VBA Select Case number to number greater than and less than

Sub ss()
Dim a As Double
a = 6.99999999
Select Case a
Case 0 To 7:
MsgBox "ok"
Case Else:
MsgBox "no"
End Select
End Sub
The Case 0 to 7 results in a check for a >= 0 and a <= 7. But what I want is a >= 0 and a < 7.
I also tried Case Is >=0, Is < 7.
How can I do this in a Select Case?
a = 6.99999999 should result in "ok"
a = 7 should result in "no"
select case true
case a >= 0 and a < 7
MsgBox "ok"
case else
MsgBox "no"
end select
But, unless you have more than two conditions, I would suggest you use an If instead.
Sub ss()
Dim a
a = 7
Select Case a
Case 7:
MsgBox "no"
Case 0 To 7:
MsgBox "ok"
Case Else:
MsgBox "no"
End Select
End Sub
Sub ss()
Dim a As Double
a = 6.99999999
Select Case a
Case 0 To 7:
If a = 7 Then
MsgBox "no"
Else
MsgBox "ok"
End If
Case Else:
MsgBox "no"
End Select
End Sub
I finally work out this solution. Thank you all!

VBA Select Case runs through without selection

I am really puzzled by this one. Searched the net already but did not find any answer related to my specific case. I have the following code:
Dim CE_res As Integer
For Index = 1 To 7
Status = ""
CE_res = CInt(Worksheets("werkblad").Range("CEres" & Index).Value)
If CE_res = 0 Then
' Everything ok
Status = "Pass"
Else
Select Case CE_res
Case CE_res < -1500
Status = "Invalid kV Value"
Case CE_res < -999
Status = "No kV Value"
Case CE_res < -399
Status = "kV too high"
Case CE_res = 0
Status = "kV too low"
Case CE_res > 500
Status = "Invalid mAs Value"
Case CE_res > 49
Status = "mAs Value missing"
Case CE_res > 9 And CE_res < 50
Status = "Adapt Target"
Case CE_res > 200
Status = "Adapt Filter"
Case Else
Status = "No Selection"
End Select
End If
The CE_res is set to 50. However, always Case Else is selected. Declared CE_res as integer, coverted Worksheets("werkblad").Range("CEres" & Index).Value to an integer, just to be sure.
However, it does not seem to execute the Select Case correctly. Tried it also with other values, even changed the case CE_res > 49 to CE_res = "50" but this also did not work.
Now, I am out of ideas what could be wrong.
You are not using the correct syntax for the Case statements
Dim CE_res As Integer
For Index = 1 To 7
Status = ""
CE_res = CInt(Worksheets("werkblad").Range("CEres" & Index).Value)
If CE_res = 0 Then
' Everything ok
Status = "Pass"
Else
Select Case CE_res
Case < -1500
Status = "Invalid kV Value"
Case < -999
Status = "No kV Value"
Case < -399
Status = "kV too high"
Case 0 ' or possibly Case < 0 ????
Status = "kV too low"
Case > 500
Status = "Invalid mAs Value"
Case > 200
Status = "Adapt Filter"
Case > 49
Status = "mAs Value missing"
Case > 9
Status = "Adapt Target"
Case Else
Status = "No Selection"
End Select
End If
Note that I had to move > 200 prior to > 49, otherwise a value of, for instance, 230 would have matched > 49 and therefore never reached your > 200.
The way you had it, the statement Case CE_res < -1500 would test if CE_res was < -1500. If it was, that would return a True which was then compared to the object of the case statement (i.e. CE_res) and, if it matched (which it wouldn't) that leg of the Select statement would execute.
As mentioned by other members, due to your use of < > = comparitors you need to make sure you order them correctly or they will not work as intended. With the very specific values you are using you would probably be better to use TO and be more specific with the values.
Select Case
Case -1000 TO -1500
'Do This
Case -400 TO -999
'Do That
Case -1 TO -399
'Do Something Else
End Select
If you do this the there is no ambiguity as to what does what, it's easier to read and the operation order doesn't matter.
You also don't need to add the CE_res to each Case statement as you already specified the variable on the Select Case CE-res line. The Correct syntax is
Select Case CE_res
Case <-1500
Status = "Invalid kV Value"
End Select