+ Reply to Thread
Results 1 to 18 of 18

Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Variable

  1. #1
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Variable

    When I declare a set of variables in Microsoft Excel's VBA Version 15.0 as such:
    Please Login or Register  to view this content.
    ...I notice in my Locals Window that i & j (or any variables except the last variable declared as any datatype) are variants; however, k is declared as type: Integer.

    When I step through my code, I see i & j change from: Variant/Empty to Variant/Integer as I assign integers to i & j. Since they're variants, I can also assign other datatypes to i & j.

    QUESTION: Is there any way to declare multiple variables per line of the same datatype without a long-winded: Dim i As Integer, j As Integer, k As Integer?

    I thought Dim i, j, k As Integer declared all i, j, and k as Integers and now I'm about 10,000 lines deep into a project I'd rather not retrace through for that. Hoping for some kind of Application.Magic command.

    Thanks in advance,

    Joe
    Last edited by joe31623; 12-05-2015 at 01:48 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    As far as I know the only way is to use what you already know:
    Dim i As Integer, j As Integer, k As Integer
    Please Login or Register  to view this content.
    What do you mean by "I'd rather not retrace through for that"
    It is only one declaration to do at the beginning and finally you can use the VBA editor to manage changes
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    I meant go through a dozen class modules and about 10,000 lines of code. Thanks for the quick response.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Hi Joe

    See this Link...
    http://www.cpearson.com/excel/declaringvariables.aspx

    From that Link..
    Pay Attention To Variables Declared With One Dim Statement

    VBA allows declaring more than one variable with a single Dim statement. I don't like this for stylistic reasons, but others do prefer it. However, it is important to remember how variables will be typed. Consider the following code:
    Dim J, K, L As Long


    You may think that all three variables are declared as Long types. This is not the case. Only L is typed as a Long. The variables J and K are typed as Variant. This declaration is functionally equivalent to the following:
    Dim J As Variant, K As Variant, L As Long


    You should use the As Type modifier for each variable declared with the Dim statement:
    Dim J As Long, K As Long, L As Long
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

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

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    I take it one step further and declare a single variable per line. That gives the opportunity to add an end-of-line comment that describes usage.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Isn't there some 'archaic' way to setup how variables are typed by default based on their name?
    If posting code please use code tags, see here.

  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    "That gives the opportunity to add an end-of-line comment that describes usage. "
    It is perhaps an "'archaic' way" ( coming from the previous century ) but it is clear, simple and force developer to document
    and it does not cost so much.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    PCI

    I wasn't commenting on your post, I just seem to recall that there was some way in VBA to have variables automatically typed.

    Perhaps I'm mistaken.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari


  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Hi Norie

    I played with the Link you referenced and this definitely appears to work...Locals Window returns the proper Data Types.

    Not certain how I'd use it as yet but I'd appreciate any feedback.

    Please Login or Register  to view this content.
    And this appears to work...
    Please Login or Register  to view this content.
    And this appears to work...
    Please Login or Register  to view this content.

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

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Norie, I'm shocked. Shocked. Ugh.

    Quote Originally Posted by jaslake View Post
    Not certain how I'd use it as yet but I'd appreciate any feedback.
    Here's some John: It's horrible.

    Unless you write code for your own entertainment, it's a miserable thing to do for the guy that follows you in the parade with the shovel and bucket.
    Last edited by shg; 12-05-2015 at 07:57 PM.

  12. #12
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Joe,
    Quote Originally Posted by joe31623 View Post
    .....
    QUESTION: Is there any way to declare multiple variables per line of the same datatype without a long-winded: Dim i As Integer, j As Integer, k As Integer?....
    Not quite what you want but a little bit better.... ... just a bit less typing

    Instead of

    Dim i As Integer, j As Integer, k As Integer?.

    then the "shorthand" alternative

    Dim i%, j%, k%


    http://www.excelforum.com/excel-prog...ml#post4194972



    Alan

    P.s. I personally would write most Declarations on one line, and add 'comments to the right to explain what they are.
    I do not think it takes much time to do that, and space / memeory is cheap for code lines, and later you ( or as shg said....the guy that follows you in the parade with the shovel and bucket.....) would be very greatful you did...

    Also the 'comments in the VB editor, like in the Code window at ExcelForum go off to the right and are not too messy in the final code,- so you only see them if you want to and scrol to the right
    Last edited by Doc.AElstein; 12-06-2015 at 12:19 PM. Reason: Wonky Typos
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  13. #13
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    @shg

    I also use this approach...
    I take it one step further and declare a single variable per line.
    Thanks for the feedback as my bucket has a hole in it...

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    shg

    I'm not suggesting using that method.

  15. #15
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    @Norie

    Yeah, I got this...
    I'm not suggesting using that method
    From these comments...
    Isn't there some 'archaic' way to setup how variables are typed by default based on their name?

    and this

    I wasn't commenting on your post, I just seem to recall that there was some way in VBA to have variables automatically typed.
    I frankly wasn't aware of this method and found it interesting...so I followed up and tested. It appears to work...

    I'll likely not use it but am now aware it exists, for whatever "archaic" purpose. Thank you for that and adding to my education.

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

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Quote Originally Posted by Doc.AElstein View Post
    then the "shorthand" alternative

    Dim i%, j%, k%
    If you're down to you last 5kB of disk space, which is the only excuse for that, consider a GoFundMe drive for 50 bucks to get a new one.

  17. #17
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Quote Originally Posted by shg View Post
    If you're down to you last 5kB of disk space, which is the only excuse for that, consider a GoFundMe drive for 50 bucks to get a new one.
    or maybe consider a free code storing facility:

    http://www.mrexcel.com/forum/excel-q...ml#post4074287
    http://www.mrexcel.com/forum/excel-q...ml#post4078702
    Last edited by Doc.AElstein; 12-07-2015 at 03:55 PM.

  18. #18
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Avoiding Variants: Multiple Declarations per Line Assign Variants To All But Last Vari

    Quote Originally Posted by shg View Post
    ..........
    Unless you write code for your own entertainment, it's a miserable thing to do for the guy that follows you in the parade with the shovel and bucket.
    Quote Originally Posted by Doc.AElstein View Post
    .......
    P.s. I personally would write most Declarations on one line, and add 'comments to the right to explain what they are.
    I do not think it takes much time to do that, and space / memory is cheap for code lines, and later you ( or as shg said....the guy that follows you in the parade with the shovel and bucket.....) would be very grateful you did.......
    Quote Originally Posted by jaslake View Post
    @shg
    I also use this approach...
    Thanks for the feedback as my bucket has a hole in it...
    Translation across the Pond for English viewers...
    Quote Originally Posted by shg View Post
    I take it one step further and declare a single variable per line. That gives the opportunity to add an end-of-line comment that describes usage.
    _ Not doing the above form of Declaration is like giving the Bloke who has to Shovel S___ uphill a Rake.


    Edit: Of course I meant Sand
    Last edited by Doc.AElstein; 12-08-2015 at 01:42 PM. Reason: Edit for .. S_hg

+ 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. Setting an object using multiple variants
    By knitterkuba in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2015, 04:36 AM
  2. Variants of csv file
    By jywll in forum Excel General
    Replies: 0
    Last Post: 02-18-2014, 05:26 PM
  3. [SOLVED] Excel 2007 : Sumifs with Multiple variants
    By ncurran217 in forum Excel General
    Replies: 1
    Last Post: 04-20-2012, 11:06 AM
  4. [SOLVED] Searching for multiple names and variants
    By CFlack8472 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-19-2012, 08:30 PM
  5. Write text to multiple cells using variants
    By therealjag in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-18-2009, 10:56 AM
  6. Use of Variants
    By rwgrietveld in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-14-2008, 08:44 AM
  7. [SOLVED] Variants vs ranges
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2005, 08:25 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