+ Reply to Thread
Results 1 to 21 of 21

Convert Vartype number to string

  1. #1
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Convert Vartype number to string

    Hello Dear Excel Forum

    Is there a default way to get string from Vartype number?
    For example:
    Debug.Print vbString ' prints 8

    I would like to convert number 8 to "vbString" or "String"
    Last edited by jakopak; 03-04-2017 at 04:32 PM.

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Convert Vartype number to string

    Do you mean something like that?
    Please Login or Register  to view this content.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: Convert Vartype number to string

    If you want to get name of the type you can use TypeName()
    Like:
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Convert Vartype number to string

    To clarify what I mean:

    When I test function with variable input for type I use:
    If Vartype(var)=vbString Then do something
    There is intelisense when I type vbS -> vbString
    vbString stores long number 8. So there must be a public type or enum which stores combination of type name and coresponding number

    vbInteger=2
    vbLong=3
    vbString = 8

    I don't want to rember the numbers so I would like to have a function which converts number to its corresponding typeName.
    So when I have for example in variable var stored number 2 or 8:
    var=2 ->"vbInteger" or "Integer"
    var=8 ->"vbString" or "String"

    For example lets have a function conv:
    Please Login or Register  to view this content.
    But I don't want to have unnecessary code in my project. Especially large Case or Ifs structures. So I'm asking if there is native way to do it.
    Last edited by jakopak; 03-04-2017 at 05:33 PM.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Convert Vartype number to string

    I don't get what you're trying to do, or how it could usefully compact your code.

    VbString is indeed 8, but so is

    xlCircle
    xlClipboardFormatBIFF
    xlColor2
    xlDBF3
    xlDialogPrint
    xlEdgeTop
    xlEPS
    xlFillYears
    xlIMEModeAlpha
    xlLessEqual
    xlLinkStatusSourceNotOpen
    xlListDataTypeListLookup
    xlListDataValidation
    xlLowerCaseRowLetter
    xlMarkerStyleCircle
    xlPaperA3
    xlParamTypeDouble
    xlPasteColumnWidths
    xlPercentOfTotal
    xlPivotCellDataPivotField
    xlRangeAutoFormatColor2
    xlReport9
    xlScrollBar
    xlSmartTagControlImage
    xlTableBody
    xlTitleBar
    xlTotalsCalculationVar
    xlTrendline
    xlVALU
    xlYDMFormat
    Last edited by shg; 03-04-2017 at 06:04 PM.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Convert Vartype number to string

    If you go to the ObjectBrowser and type "vbString" in the search field, it will take you to the vbVarType class which lists all of the types that are returned by VarType function.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  7. #7
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Convert Vartype number to string

    shg, in code I use vbString, vbLong... for testing input variable. When variable doesn't meet criteria it should write log or debug.print message. If I use vbString I get number in log or in debug.print message. The number tells me nothing. I don't want to remember whole table. It would be just convenient if someone can write something like this:
    Debug.Print toname(vbDouble)
    and get "vbDouble" as result.

    Also your list of variables brings me to question, How have you compiled it, please?

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Convert Vartype number to string

    easiest is to use a function to convert it
    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  9. #9
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Convert Vartype number to string

    Pike, I know. That is something I would use as last resort. I think its unfeasible to write such functions for each group of xlVariables or vbVariables.

  10. #10
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Convert Vartype number to string

    Quote Originally Posted by jakopak View Post
    Pike, I know. That is something I would use as last resort. I think its unfeasible to write such functions for each group of xlVariables or vbVariables.
    That is one of the uses of the Object Browser, to find the classes of VBA constants.

  11. #11
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Convert Vartype number to string

    if you are using Vartype that one function is all you will need
    https://msdn.microsoft.com/en-us/lib...ffice.15).aspx

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Convert Vartype number to string

    Is there a problem with kasan's suggestion of using Typename instead of Vartype. After all, it returns the name of the variable type.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  13. #13
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Convert Vartype number to string

    Good thought Typename is for Office 2013 and later, jakopak MS-Off Ver:2010
    Last edited by pike; 03-05-2017 at 08:39 PM.

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Convert Vartype number to string

    TypeName goes back to at least Excel 2003 (the earliest version I have installed to check).

  15. #15
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Convert Vartype number to string

    My Excel 2005 had TypeName.

  16. #16
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Convert Vartype number to string


  17. #17
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Convert Vartype number to string

    If you click other versions at the top of that page and then 2010, you'll see it applied there too. That's just a limitation of how far back MS maintains its documentation, I think.

    As mentioned, typename has been part of VB since at least VB3 and has been part of Office since version 5/95.

  18. #18
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Convert Vartype number to string

    I know function typeName and VarType and use it heavily. Problem with Kasan solution is that if try
    Debug.Print TypeName(CStr(vbDouble)) I get "String" but I would like to get "vbDouble".
    Function which Pike provided in #8 will essentially do what I want but I suspect,
    there is a native way to do it. Because why not?

    Maybe the xlValues are strored in Enum. Values names in enum can't be accessed too by number or can they?

    SHG you have provided list of values which has value 8 in answer #5. I believe you have constructed it programmatically. Am I right?
    If so, can you please provide the method?

    xlCircle
    xlClipboardFormatBIFF
    xlColor2
    xlDBF3
    Last edited by jakopak; 03-06-2017 at 05:57 AM.

  19. #19
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Convert Vartype number to string

    I did not make post #5.

    Why do you wish to use Vartype on a variable and then somehow convert that result to the name of the type, instead of simply using TypeName on the variable?

    If you wish to access Enum values from a library, you need an additional library like tlbinf which is not a standard library so your code is unlikely to work on any other machine. Using a Select Case approach is simpler, though I still cannot imagine the need. Most often, when questions like this are asked, it seems that the asker is fighting to try to make VBA behave like some other language rather than working with VBA.

  20. #20
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Convert Vartype number to string

    I am sorry for confusion answer #5 provided SHG.

    My motivation for example: I have defined an array of types.
    ara1(0)=vbDecimal
    ara1(1)=vbInteger
    ara1(2)=vbLong
    ara1(3)=vbDouble

    Now I do some operation on this array and put result in another array ara2
    Debug.Print ara2(0) -> 8
    Debug.Print ara2(1) -> 7
    Debug.Print ara2(2) -> 8
    Debug.Print ara2(3) -> 1

    Those numbers in debug message tells me nothing although I already unintentionally remember them. But I don't want to remember it for all groups of xl and vb values.

    Quote Originally Posted by xlnitwit View Post
    Most often, when questions like this are asked, it seems that the asker is fighting to try to make VBA behave like some other language rather than working with VBA.
    That may be my problem. I come from R background. I need this functionality for exploring VBA and debugging higher order functions (functors) and metaprogramming.
    Last edited by jakopak; 03-06-2017 at 06:45 AM.

  21. #21
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Convert Vartype number to string

    There is no native method to do that. You will have to write your own function one way or another, or adopt a different approach to whatever you are doing with the array so that you can store actual type names rather than enum values.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Should i convert string to number before comparing against a number or leave as is
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2015, 12:04 PM
  2. [SOLVED] convert string to number
    By pannam in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-11-2014, 05:25 AM
  3. Convert string to number
    By djbomaha in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-27-2012, 04:40 PM
  4. Convert a string of letters to a number
    By niki_cboy in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-29-2012, 01:25 AM
  5. Replies: 1
    Last Post: 01-25-2010, 12:48 PM
  6. Convert a number to a name string
    By Numbers to name string in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-23-2006, 11:55 AM
  7. Convert a String to an number
    By 11Oppidan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2005, 12:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1