Consulting

Results 1 to 9 of 9

Thread: Combine multiple DIMs into one line instead of separate?

  1. #1

    Combine multiple DIMs into one line instead of separate?

    Hi,
    I'm wondering if I'm actually doing this correct or not?

    Instead of declaring my DIMs on seperate lines like:

    Dim x As Integer
    Dim LastRow As Integer
    Dim i As Integer
    I would do this instead....

    Dim x, LastRow, i As Integer
    Is this correct?? Because when I step through my code with F8, I notice that they are set as Variable/Empty in the Locals window?? Not set as Integer ?

    Am I doing it correctly? Or should I be doing it differently?

    Thanks
    Last edited by ashleyuk1984; 11-23-2017 at 08:18 AM.

  2. #2
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    From : http://www.cpearson.com/excel/declaringvariables.aspx


    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
    Another resource : http://learnexcelmacro.com/wp/2012/1...dim-statement/


    And finally from the 'creator' itself : https://msdn.microsoft.com/en-us/vba...ring-variables

    In the following statement, the variables intX, intY, and intZ are declared as type Integer.

    Dim intX As Integer, intY As Integer, intZ As Integer
    In the following statement, intX and intY are declared as type Variant, and only intZ is declared as type Integer.
    Dim intX, intY, intZ As Integer
    You don't have to supply the variable's data type in the declaration statement. If you omit the data type, the variable will be of type Variant.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Yes, but not exactly -- depends on what you mean by 'right' -- see Logit's post and links, esp the part about variables not explicitly Dim-ed as a specific type



    To ensure that the macro works as intended, I think (IMVHO) it's better defensive programming to explicitly Dim everything, even on one line


    Dim x as Integer, LastRow as Integer, i As Integer

    That way later in the macro if I make a mistake and use

    .....
    
    LastRow = "Done"
    
    .....
    
    LastRow = LastRow + 1
    I'll get a message on the assignment line since "Done" cannot convert to a number
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Ahhhh, that's where I'm going wrong.
    Ok that makes sense. I was sure that I saw somewhere that you could declare them on one line just for 'neatness'... But I was doing it slightly wrong.

    Dim x As Integer, LastRow As Integer, i As Integer
    This is the approach that I was looking for.

    Thank You Logit and Paul !

  5. #5
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    If you want, you can also use type declaration characters:

        Dim x%, LastRow%, i%
    or even a Def type statement like

    DefInt I
    then any variable whose name begins with i is automatically an Integer unless otherwise specified.

    Just as an aside, you should always use Long for row number variables.
    Be as you wish to seem

  6. #6
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    613
    Location
    .
    You are welcome. Glad to help.

    For what its worth, I sometimes 'forget' and do it wrong as well.

    POO!

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    IMNSHO It is best programming practice to declare all on separate lines, grouped by subject, then alphabetized.

    Makes it easy to research when you next have to maintain the code.

    SamT's Rule # 1: Write your code like an instruction manual.
    Rule # 2, If you don't understand Rule #1, Write your code like Microsoft.

    Also, if I use mnemonic Names, I comment the meaning for accurate mental association
    Dim EmpFNm As String 'Employee First Name
    Dim EmpLNm As String  'Employee Last Name
    Dim EmpA1 as String 'Employee First or Street Address
    Dim EmpA2 As String 'Employee Address 2 or Extra line
    Dim EmpA3 As String  'Employee City
    Dim EmpA5 As String(2) 'Employee State Abbreviation
    Dim EmpA6 As String(10) 'Employee full Zip Code (12345-1234)
    Last edited by SamT; 11-24-2017 at 12:11 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    @SamT -- Syntax for fixed length strings was wrong

    I'd probably use classes, but if you wanted to keep it more simple, you could use user defined Types

    Option Explicit
    
    Type Employee
        FirstName As String
        LastName As String
        StreetAddress1 As String
        StreetAddress2 As String
        City As String
        State As String * 2
        ZipCode As String * 5
        MgrFirstName As String
        MgrLastName As String
    End Type
    
    Sub TypeDemo()
        Dim Manager As Employee, Worker As Employee
        
        With Manager
            .FirstName = "Tom"
            .LastName = "Jones"
        End With
        
        With Worker
            .MgrFirstName = "Tom"
            .MgrLastName = "Jones"
            
            .FirstName = "Mary"
            .LastName = "Smith"
            .StreetAddress1 = "123 W. 4th Ave"
            .City = "Hometown"
            .State = "DC"
            .ZipCode = "12345"
        End With
        
        With Worker
            MsgBox .FirstName & " " & .LastName & " works for " & .MgrFirstName & " " & .MgrLastName
        End With
        
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    String size format? I figgered. But it's valid in some language. I just look it up when I rarely need it

    And I too love the simpleness and capabilities of Classes.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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