VBA to define variable using if then

chroniclesofdave

New Member
Joined
Aug 8, 2016
Messages
42
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:

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,133
Office Version
365
Platform
Windows
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:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,200
Office Version
365
Platform
Windows
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.
 

chroniclesofdave

New Member
Joined
Aug 8, 2016
Messages
42
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:

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,133
Office Version
365
Platform
Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,200
Office Version
365
Platform
Windows
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
 

Forum statistics

Threads
1,077,772
Messages
5,336,150
Members
399,067
Latest member
CJWFM

Some videos you may like

This Week's Hot Topics

Top