?
Solved

How to determine data type of variable

Posted on 2011-09-29
6
Medium Priority
?
554 Views
Last Modified: 2012-05-12
I want to create a flexible function that returns a result based on a formula by Application.Evaluate(Name)  The problem is around handling both the following two situations:

Application.Evaluate("Sheet1!A:A") -- returns an object
Application.Evaluate("LEFT(""test"",2)") -- doesn't

I have no good way of predicting beforehand if the result will be an object or not and I'd prefer not to use a On Error Resume... type solution if there is a better way - which may just mean a better understanding of how the varType function works.

varType(Application.Evaluate("Sheet1!A:A")) returns "8204" but this was not defined as a range in vba help (http://msdn.microsoft.com/en-us/library/aa263402(v=vs.60).aspx)

How do I make this robust?

Many thanks.



vCommandFinal is a string/variant
getEvaluateFormula is a variant
rowContext is an array that returns an integer based on the specified worksheet.index property
Select Case VarType(Application.Evaluate(vCommandFinal)) 
      Case vbObject
        Set vResult = Application.Evaluate(vCommandFinal)
        getEvaluateFormula = vResult(rowContext(vResult.Worksheet.Index))
      Case vbError
        Stop
        vResult = "Unknown or Invalid"
      Case Else
        Set vResult = Application.Evaluate(vCommandFinal)
        Debug.Print VarType(vResult)
        getEvaluateFormula = vResult
      End Select

Open in new window

0
Comment
Question by:_Benaiah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:dlmille
ID: 36890006
is there a reason you don't just use a variant variable to catch anything coming off the Evaluate command?  Unless you're parsing unknowns, the program you're writing should expect the datatype its looking for.  I'm not sure I follow why you'd need to do this.

Can you elaborate more?

Dave
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 36890126
If you check the bottom of the article you cited, you will be able to work out that 8204 is an array of variants. (8192 for array + 12 for variant). If you used TypeName, you would get "Range".

Regards,
Rory
0
 
LVL 1

Author Closing Comment

by:_Benaiah
ID: 36890206
TypeName looks like exactly what I wanted
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:_Benaiah
ID: 36890263
btw whats the difference?
0
 
LVL 1

Author Comment

by:_Benaiah
ID: 36890275
...meaning when is it best to use varType over TypeName?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 36890278
In theory, the main difference is that one returns a number code and the other a type string.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question