+ Reply to Thread
Results 1 to 5 of 5

Type conversion, single value converts Variant to String

  1. #1
    Registered User
    Join Date
    01-31-2020
    Location
    Chicago, USA
    MS-Off Ver
    2016
    Posts
    17

    Type conversion, single value converts Variant to String

    Why does a variant with a single value type convert to string?

    I have filtered a list in another sub function, and then I want to parse out the values in column A. It works fine if there are multiple values, but when the variant "temp" has only one item, it becomes a string and I get Type Errors when it gets to the For/Next loop.

    I tried to force it back to a variant with the red code, but it still comes out as a string!

    Please Login or Register  to view this content.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Type conversion, single value converts Variant to String

    Variant is variable type. So variant temp can hold any given type.

    Once temp is assigned some value/object type of variable held is implicitly set to that of the value/object.

    What is the purpose of using CVar()?

    If variable is declared as variant then you can use it below without issue. If it's declared as specific type, it can only hold value/object of that type.
    Ex:
    Please Login or Register  to view this content.
    EDIT: Personally, I've never seen code or algorithm which required use of CVAR().
    Last edited by CK76; 01-31-2020 at 12:05 PM.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Type conversion, single value converts Variant to String

    rather than it's type check whether it's a single item or not.

    Please Login or Register  to view this content.
    To save duplicating your 'parse a bunch of stuff' put that code in another routine
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Type conversion, single value converts Variant to String

    Oh, if you need to convert string to single item array.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-31-2020
    Location
    Chicago, USA
    MS-Off Ver
    2016
    Posts
    17

    Re: Type conversion, single value converts Variant to String

    Thank you,

    I'm new to VBA and I guess I didn't fully understand what a Variant is. Andy Pope's solution worked perfectly for me. Thank you all.

    Edit: Thank you for the suggestion to convert to array, I think that was what I was trying to accomplish with CVar()

+ 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. Single string into a variant issue
    By Scoobster_doo in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-09-2017, 10:26 AM
  2. [SOLVED] maximum length of a string / variant type
    By GeneralDisarray in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2014, 12:12 PM
  3. Replies: 0
    Last Post: 12-12-2013, 08:29 PM
  4. Problem with Type conversion- String to Int
    By meen2525 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-31-2011, 09:23 AM
  5. convert textbox value from string to either single or double numeric data type
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2011, 06:40 PM
  6. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2005, 01:54 AM
  7. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 12:30 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