1

I would like to use different data types with same variable and do some computation. For example, I am using the variable "option" in,

If option = 1 then
   Do this
Elseif option = "X" then
   Do this
Elseif option = 6 then
   Do this
Endif

Is there any way i can get this working ?

2
  • how are the values getting assigned to "option"? Would it be possible to reference 1 and 6 as strings? The other option may be to have another variable that holds "X" and if that variable is set to a value, set option to 0... Commented Jul 2, 2013 at 12:32
  • I am reading the options from a row. possible options may be 1, X & 2 for one set, Integers 0 to 6, -1 to -6 for another set. I want to use the same variable here. So that for the first set i can consider the options as string and for set 2 as Integer. For the set 2, I would like to use the condition like, If option >0 or option = 0 or option <0 to cover all the possible values of -1 to -6, 0 and 1 to 6. Kindly advice. Commented Jul 2, 2013 at 12:39

2 Answers 2

2

You can use a variant to allow for different data types inside of one variable. The below code works.

Public Sub TestVariant()
  Dim x As Variant

  x = 17

  If x = 1 Then
    ' Do this
  ElseIf x = "bob" Then
    ' Do this
  ElseIf x = 17 Then
    ' This is done
  End If
End Sub

Off the top of my head, this smells a bit though. It would be better to have the variable be strictly defined. If you are dealing with numbers that could also be strings, you can always just make the numbers into strings. For example:

Public Sub TestVariant()
  Dim x As string

  x = "1"

  If x = "1" Then
    ' Do this
  ElseIf x = "bob" Then
    ' Do this
  ElseIf x = "17" Then
    ' This is done
  End If
End Sub

Edit: Another example which compiles and works as expected in Excel.

Public Function TestVariant(theOption As Variant)
  If theOption < 0 Then
    TestVariant = "Negative"
  ElseIf theOption = 6 Then
    TestVariant = "Six"
  ElseIf theOption = "Stuff" Then
    TestVariant = "Other Stuff"
  Else
    TestVariant = theOption
  End If
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

I followed the same method. My function is like this, Function fcComm(oddsId As Integer, agt As String, ma As String, sma As String, Option As Variant). And i am using the same function in excel as =fcComm(A3,B4,C4,D4, -6). My condition inside the function is If Option < 0 then Do this. However i jus receive #VALUE error in cells. What could be the issue ? I have used the same function for different condition like If Option = 1 and it works well.
I assume you aren't actually using the variable option as that is reserved and it doesn't really work. It sounds like you have another error. If you look at my edit, I was able to get it to work in Excel. Try posting more of the actual code.
1

If you declare option as Variant you should be good to go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.