if then else

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hi, everyone.
I am trying to use if then else but results are wrong
on "B1" I just type "f" or "m" mean female or male and on c7 I want the constant divided by 2 for f, and by 3 for male, I tried this
HTML:
Option Explicit
Sub test()
Dim f As String, m As String
Dim constant As Integer
Dim result As Integer
Dim gender As String
    gender = Range("b1").Value
    
    constant = 500
    f = Range("b1").Value
    m = Range("b1").Value

 If gender = f Then
    result = constant / 2
    Else
    m = constant / 3
    result = m
    
    End If
    
    Range("c7").Value = result
    
    
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Your macro confuse me :P You are overthinking it.


Code:
Sub test()

If Range("b1").Value = "f" Then
        Range("c7").Value = 500 / 
End If
 If Range("B1").Value = "m" Then
        Range("c7").Value = 500 / 3
End If
End Sub
 
Upvote 0
Hi, everyone.
I am trying to use if then else but results are wrong
on "B1" I just type "f" or "m" mean female or male and on c7 I want the constant divided by 2 for f, and by 3 for male, I tried this
HTML:
Option Explicit
Sub test()
Dim f As String, m As String
Dim constant As Integer
Dim result As Integer
Dim gender As String
    gender = Range("b1").Value
    
    constant = 500
    f = Range("b1").Value
    m = Range("b1").Value

 If gender = f Then
    result = constant / 2
    Else
    m = constant / 3
    result = m
    
    End If
    Range("c7").Value = result
End Sub

In your macro you wrote.

Code:
gender = Range("b1").Value

and then

Code:
f = Range("b1").Value

and THEN

Code:
 If gender = f Then

.... This will always be true.. so It wont work as you want it to.
 
Upvote 0
A proper way of using IF/ELSE, is stating, if cell B1 is "f", then do something, if its anything other then "f" then do something else...


Code:
Sub test()If Range("b1").Value = "f" Then
        Range("c7").Value = 500 / 2
    Else '
    MsgBox "Something else....."
End If
End Sub
 
Upvote 0
Or you can use a Case statement which seems a bit more readable.
VBA Code:
Sub GenderValue()

    Dim Gender   As String
    Dim Result   As Long

    Const cValue As Long = 500

    Gender = Range("B1")

    Select Case UCase(Gender)
        Case "F": Result = cValue / 2
        Case "M": Result = cValue / 3
    End Select

    Range("C7") = Result

End Sub

Note: I use UCase to remove the case sensitivity.
 
Last edited by a moderator:
Upvote 0
Solution
GAVIN thank you so much, now I will insert the real calculation, thanks again.
 
Upvote 0

Forum statistics

Threads
1,221,497
Messages
6,160,151
Members
451,625
Latest member
sukhman

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