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
Related
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
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
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
I am trying to make a macro that returns a given rank depending on the value in a cell. The value it is based on (B24) would determine the ranking and place it in B26. Below is the ranking and the code I am using. Ex. Values with over 2B should result in "1". How can I get this to work? I am currently getting "Compile error: Else without if"
Sub Criteria()
On Error GoTo catch_error
Worksheets("Sheet1").Activate
Dim score As Integer, result As String
score = Range("B24").Value
If score > 2000000000 Then result = "1"
ElseIf score >= 1500000000 And score <= 1999999999.99 Then result = "2"
ElseIf score >= 500000000 And score <= 1499999999.99 Then result = "3"
ElseIf score >= 250000000 And score <= 499999999.99 Then result = "4"
ElseIf score < 249999999.99 Then result = "Out Of Scope"
result = Range("B26").Value
Exit Sub
catch_error:
MsgBox "Some Error Occurred"
End Sub
A single line If statement can't have an Else:
If score > 2000000000 Then result = "1"
You need to restructure it like this:
If score > 2000000000 Then
result = "1"
ElseIf score >= 1500000000 And score <= 1999999999.99 Then
result = "2"
ElseIf score >= 500000000 And score <= 1499999999.99 Then
result = "3"
ElseIf score >= 250000000 And score <= 499999999.99 Then
result = "4"
ElseIf score < 249999999.99 Then
result = "Out Of Scope"
End If
Write "End if" on the line before result = Range("B26")...
Dim score As Double, not as Integer, your values are too high and they are not integers.
This should be reversed:
Range("B26").Value = result
As to what the others said:
You cannot use a single line if with Else or Else if
Score needs to be a double
The last line is reversed.
and to add
The less than arguments are not needed as once the if statment finds a true it stops looking.
Code:
Sub Criteria()
On Error GoTo catch_error
With Worksheets("Sheet1")
Dim score As Double, result As String
score = .Range("B24").Value
If score > 2000000000 Then
result = "1"
ElseIf score >= 1500000000 Then
result = "2"
ElseIf score >= 500000000 Then
result = "3"
ElseIf score >= 250000000 Then
result = "4"
Else
result = "Out Of Scope"
End If
.Range("B26").Value = result
Exit Sub
End With
catch_error:
MsgBox "Some Error Occurred"
End Sub
I have a table TEST with columns VALUE,VALUE_SIM,SIM_STATUS,ID. I want to update the column SIM_STATUS for the ID = 288. I also want to display the columns after updating.
The conditions are :
1. SIM_STATUS = 0 when VALUE = VALUE_SIM.
2. SIM_STATUS = 1 when VALUE < VALUE_SIM.
3. SIM_STATUS = 2 when VALUE > VALUE_SIM.
I wrote the following query but it is showing an error.
("UPDATE TEST"
"SET SIM_STATE = ( CASE WHEN VALUE = VALUE_SIM THEN SIM_STATE = 0 END )"
"SET SIM_STATE = ( CASE WHEN VALUE < VALUE_SIM THEN SIM_STATE = 1 END )"
"SET SIM_STATE = ( CASE WHEN VALUE > VALUE_SIM THEN SIM_STATE = 2 END )"
"where ID = 288 ");
The query that you want is:
UPDATE TEST
SET SIM_STATE = (CASE WHEN VALUE = VALUE_SIM THEN 0
WHEN VALUE < VALUE_SIM THEN 1
WHEN VALUE > VALUE_SIM = 2
END)
WHERE NUMBER = 288;
Your query has several syntax errors. I don't even know if you intend for the double quotes to be part of the query.
This would do i guess
UPDATE TEST
SET
SIM_STATE =
CASE WHEN VALUE < VALUE_SIM THEN 1
+ CASE WHEN VALUE > VALUE_SIM THEN 2
+ CASE WHEN VALUE = VALUE_SIM THEN 0
WHERE ID = 1