VBA to define variable using if then

chroniclesofdave

New Member
Joined
Aug 8, 2016
Messages
48
I am wanting to have a outlook email address to be set based on the value of Combobox2. What i have doesn't seem to function, and i am sure it is simple but i am a little stuck. any suggestions would be great. What i have so far is:
Code:
Dim name As String
If ComboBox2.Value = "Bill" Then Set carrier = "Bill@gmail.com"
ElseIf ComboBox2.Value = "George" Then Set carrier = "George@icloud.com"
End If
 
Last edited by a moderator:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You only use the "Set" keyword when setting objects like ranges, not variables like strings, i.e.
Code:
Dim name As String
If ComboBox2.Value = "Bill" Then 
    carrier = "Bill@gmail.com"
ElseIf ComboBox2.Value = "George" Then carrier = "George@icloud.com"
End If
 
Last edited:
Upvote 0
You only set Objects not strings, so remove the word "Set".
Also you are declaring a variable called name, but then using a variable called carrier.
 
Upvote 0
Right, i had copied older code, what i will test in a moment is
Code:
 [FONT=Verdana]Dim name As String[/FONT]
If ComboBox2.Value = "Bill" Then name = "Bill@gmail.com"
ElseIf ComboBox2.Value = "George" Then name = "George@icloud.com" 
end if
 
Last edited:
Upvote 0
Best practices says not to use reserved words (names of existing functions, properties, and methods), like "name", as the name of variable, procedures, or functions.
It can cause ambiguity, confusion, errors, and/or unexpected results.
 
Upvote 0
Along with Joe4's comments, the if statement should be like
Code:
Dim Nme As String
If ComboBox2.Value = "Bill" Then
    Nme = "Bill@gmail.com"
ElseIf ComboBox2.Value = "George" Then
    Nme = "George@icloud.com"
End If
 
Upvote 0

Forum statistics

Threads
1,213,491
Messages
6,113,963
Members
448,536
Latest member
CantExcel123

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