Solved

How to determine data type of variable

Posted on 2011-09-29
6
478 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
  • 3
  • 2
6 Comments
 
LVL 41

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 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now