VBA- Does not equal IF Statement

brian662

New Member
Joined
Feb 8, 2013
Messages
6
Hi I have the following code to either add, subtract, multiply or divide two numbers

Private Sub Cmd_Test_Click()
Worksheets("Sheet1").Range("C9") = "1st Value"
Worksheets("Sheet1").Range("D9") = "2nd Value"
Worksheets("Sheet1").Range("E9") = "Operation"
Worksheets("Sheet1").Range("F9") = "Result"


Dim sngMy1stValue As Single
Dim sngMy2ndValue As Single
Dim strMyOperation As String


sngMy1stValue = Worksheets("Sheet1").Range("C10").Value
sngMy2ndValue = Worksheets("Sheet1").Range("D10").Value
strMyOperation = Worksheets("Sheet1").Range("E10").Text


If strMyOperation = "+" Then
Worksheets("Sheet1").Range("F10").Value = sngMy1stValue + sngMy2ndValue
End If


If strMyOperation = "-" Then
Worksheets("Sheet1").Range("F10").Value = sngMy1stValue - sngMy2ndValue
End If


If strMyOperation = "*" Then
Worksheets("Sheet1").Range("F10").Value = sngMy1stValue * sngMy2ndValue
End If


If strMyOperation = "/" Then
Worksheets("Sheet1").Range("F10").Value = sngMy1stValue / sngMy2ndValue
End If


End Sub


I am trying to write an additional statement that sets the “Result” cell to 0 (zero) if a user does not enter one of the four basic mathematical operators, I know that in one area of the code, Value would equal 0, but I am not sure where to proceed with the strMyOperation = .......

Very grateful if you could help me out
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to the board..

Check out the Evaluate function,

all those IF's can be replaced with
Worksheets("Sheet1").Range("F10").Value = Evaluate("=" & sngMy1stValue & strMyOperation & sngMy2ndValue)


Then use select case to verify a valid operator was selected.

Try
Code:
Private Sub Cmd_Test_Click()
Worksheets("Sheet1").Range("C9") = "1st Value"
Worksheets("Sheet1").Range("D9") = "2nd Value"
Worksheets("Sheet1").Range("E9") = "Operation"
Worksheets("Sheet1").Range("F9") = "Result"

Dim sngMy1stValue As Single
Dim sngMy2ndValue As Single
Dim strMyOperation As String

sngMy1stValue = Worksheets("Sheet1").Range("C10").Value
sngMy2ndValue = Worksheets("Sheet1").Range("D10").Value
strMyOperation = Worksheets("Sheet1").Range("E10").Text
Select Case strMyOperation
    Case "+", "-", "*", "/"
        Worksheets("Sheet1").Range("F10").Value = Evaluate("=" & sngMy1stValue & strMyOperation & sngMy2ndValue)
    Case Else
        MsgBox "you didn't select a valid operator"
End Select
End Sub
 
Upvote 0
The only problem is I need the result to equal 0 when a user does not enter a valid operator, although the code is good, it does not answer the question on how to change the result to 0 when a invalid operator is entered, could you please help on that?
 
Upvote 0
Replace this line

MsgBox "you didn't select a valid operator"

with

Worksheets("Sheet1").Range("F10").Value = 0
 
Upvote 0
Can I just ask, is there anyway to perform this without using the SELECT CASE Block, and keeping the IF statements, and just adding another IF statement to set the result to zero when an invalid operator is entered?
 
Upvote 0
Try making it 1 If statement, and using ELSEIF

Code:
Private Sub Cmd_Test_Click()
Worksheets("Sheet1").Range("C9") = "1st Value"
Worksheets("Sheet1").Range("D9") = "2nd Value"
Worksheets("Sheet1").Range("E9") = "Operation"
Worksheets("Sheet1").Range("F9") = "Result"

Dim sngMy1stValue As Single
Dim sngMy2ndValue As Single
Dim strMyOperation As String

sngMy1stValue = Worksheets("Sheet1").Range("C10").Value
sngMy2ndValue = Worksheets("Sheet1").Range("D10").Value
strMyOperation = Worksheets("Sheet1").Range("E10").Text

If strMyOperation = "+" Then
    Worksheets("Sheet1").Range("F10").Value = sngMy1stValue + sngMy2ndValue
ElseIf strMyOperation = "-" Then
    Worksheets("Sheet1").Range("F10").Value = sngMy1stValue - sngMy2ndValue
ElseIf strMyOperation = "*" Then
    Worksheets("Sheet1").Range("F10").Value = sngMy1stValue * sngMy2ndValue
ElseIf strMyOperation = "/" Then
    Worksheets("Sheet1").Range("F10").Value = sngMy1stValue / sngMy2ndValue
Else
    Worksheets("Sheet1").Range("F10").Value = 0
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,378
Members
448,955
Latest member
BatCoder

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