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
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 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
I have a problem with my blackjack game in vb.net. This code I have will add the player's score perfectly, but when it comes to the dealer's score, it will not. It only takes the second card that the dealer has.
It is called with this:
addScore("p") 'add player's score
addScore("d") 'add dealer's score
And this is "addScore()":
Public Function card(player As String, index As Integer) As Label
Try
If player = "p" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "YouCard" & index.ToString()).Single()
ElseIf player = "d" Then
Return GroupBox1.Controls.OfType(Of Label).Where(Function(l) l.Name = "DealerCard" & index.ToString()).Single()
End If
Catch
Return Nothing
End Try
End Function
Public Sub addScore(ByVal player As String)
Dim currScore As Integer
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (currScore + 11) <= 21 Then
result = currScore + 11
ElseIf card(player, value).Text = "A" AndAlso (currScore + 1) <= 22 Then
result = currScore + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = currScore + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = currScore + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub
currScore shouldn't be there. Replace it with result
Public Sub addScore(ByVal player As String)
Dim result As Integer = 0
'Add Score
For value As Integer = 1 To 7
If card(player, value).Text = "A" AndAlso (result + 11) <= 21 Then
result = result + 11
ElseIf card(player, value).Text = "A" AndAlso (result + 1) <= 22 Then
result = result + 1
ElseIf IsNumeric(card(player, value).Text) Then
result = result + CInt(card(player, value).Text)
ElseIf card(player, value).Text = "" Then
result = result
Else
result = result + 10
End If
If player = "p" Then
YouScore.Text = result
Else
DealerScore.Text = result
End If
Next
End Sub
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
Please tell me how to repeat the if condition. In the else line I have displayed to the user to re-enter the value. Therefore I need to repeat the if condition until the value is 1 or 2 or 3.Please explain how to do I'm a newbie.
You can use infinite While-loop ,and put "Exit While" on the end of conditional statement you agreed , so there are few ways out from infinite loop.
For example , on your code:
Console.WriteLine("Please Enter the number")
Dim number As Integer = Console.ReadLine()
If (number=< 40) Then
number = number* 10
ElseIf (number=< 150) Then
number= number* 15
Else
number= number* 26
End If
Console.WriteLine(number)
Dim total As Integer
Dim vALUE As Integer
'infinite loop until user input 1,2 or 3
While True
Console.WriteLine("Please, type 1 for x . Type 2 for y. Type 3 z")
vALUE = Console.ReadLine()
If vALUE = 1 Then
Console.WriteLine("x")
total = number* (106 / 100)
Exit While 'condition matched , break from While
ElseIf vALUE = 2
Console.WriteLine("y")
total = number* (112 / 100)
Exit While 'condition matched , break from While
ElseIf uSERvALUE = 3
Console.WriteLine("z")
total = number* (116 / 100)
Exit While 'condition matched , break from While
Else
Console.WriteLine("Sorry please re-enter the value")
vALUE = Nothing
End If
End While
'do further more you need
This is my first time coding from scratch and a very dumb question indeed.
Why do i keep getting this error?
Runtime Error '13' : Type mismatch
My value in B1 is an integer
Private Sub Workbook_Open()
xdate = Worksheets("sheet1").Range("A1")
lsdate = DateAdd("d", -7, Date)
'MsgBox lsdate
If Day(lsdate) > Day(xdate) Then
Worksheets("sheet1").Range ("B1") * 1 = result
Else
Worksheets("sheet1").Range ("B1") * 1.07 = result
End If
Range("c1").Value = result
End Sub
I have further edit my code to the following but not able to get the condition to work
'To create the following condition
'If less than 7 days interest = 0%
'if 8 to 30 days interest = 7%
'if more than 31 days interest = 9%
Sub Workbook_Open()
For i = 1 To 3 'Rows.Count
xdate = Cells(i, 1)
'MsgBox Cells(i, 1)
nulldate = DateAdd("d", -7, Date)
irate7late = DateAdd("d", -8, Date)
irate7early = DateAdd("d", -30, Date)
If Day(nulldate) < Day(xdate) Then
result = Cells(i, 2) * 1
ElseIf Day(irate7early) <= Day(xdate) And Day(xdate) <= Day(irate7late) Then
'30/9/2015 20/10/2015 20/10/2015 22/10/2015
result = Cells(i, 2) * 1.07
ElseIf Day(irate7early) > Day(xdate) Then
result = Cells(i, 2) * 1.09
End If
Cells(i, 3).Value = result
Next i
End Sub
You seem pretty new at programming, so I'll explain plainly :
When you are trying to assign a value to a variable in almost every language, you use :
variable = value
So you can do a lot of operations on the value, which is on the right of the equals =.
BUT you canNOT do operations on the left of the equals = when you simply assigning a value to a variable. You can do almost any operations when you are testing 2 variables but you'll have a keyword like If or While at the start of the line of code.
So the issue with your code is that you reversed the order of the value and the variable and tried to do value * 1 = variable
Here is your corrected code (I indented it so that it can be read easily) :
Private Sub Workbook_Open()
xdate = Worksheets("sheet1").Range("A1")
lsdate = DateAdd("d", -7, Date)
'MsgBox lsdate
If Day(lsdate) > Day(xdate) Then
result = Worksheets("sheet1").Range ("B1") * 1
Else
result = Worksheets("sheet1").Range ("B1") * 1.07
End If
Sheets("Sheet_name").Range("c1").Value = result
End Sub
And you should always reference the sheet you are working with, because previously, your code didn't specify on which sheet the C1 was supposed to be.
So here, just change Sheet_name to whatever the name of your sheet is!
variables need to get the values from sheet. You are trying opposite way which is why you getting error.
Private Sub Workbook_Open()
xdate = Worksheets("sheet1").Range("A1")
lsdate = DateAdd("d", -7, Date)
'MsgBox lsdate
If Day(lsdate) > Day(xdate) Then
result = Worksheets("sheet1").Range("B1") * 1
Else
result = Worksheets("sheet1").Range("B1") * 1.07
End If
Range("c1").Value = result
End Sub
Sub test1()
Dim var1 As Variant
' If you need to get values from the range("D1") Then use the below code
var1 = Worksheets("sheet1").Range("D1").Value
End Sub