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:

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
52,070
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
35,588
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
52,070
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
35,588
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,089,377
Messages
5,407,904
Members
403,169
Latest member
Luna17

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top