Consulting

Results 1 to 5 of 5

Thread: Declaring Variable Type - Excel 2013

  1. #1

    Question Declaring Variable Type - Excel 2013

    Hello, I have a question regarding declaring the variable type. My code is comparing two sets of data against each other and creating a separate list of any differences found. The data type can be a mix of either a number, number with a letter, or a number with a letter and special character. (i.e. 1, 1-2, 1A, 1A-2) My code is currently working fine as is, and until recently I didn't give this much thought. But now I am wondering if I should be doing something different. Currently, the variable that is used to compare each cell value is declared as a String. So my question is this. Is this, or can this be an issue declaring the variable as a String if the cell value is a number or a number mixed with a letter? I know I said my code is working, but I just want to hear other thoughts on this to know if I SHOULD change something.

    As always, thanks for the help/input.

    -David

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    1. If it's working don't mess with it

    2. Excel / VBA will cast a number to a string so IMHO you're fine
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    I would abstain from declaring any variable at all and remove option explicit.
    You'll notice the code will still be running flawlessly.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Been discussed here many times

    If you're a perfect typist and never make a mistake, you might be able to get away with it

    For me, I like as much of the the computer's help as I can get

    For ex, I'd have to look hard to see where the bugs are below

    'Option Explicit -- commented out
    Sub test()
        msg1 = "message"
        msg2 = "another"
    
    
        MsgBox msg2 & " " & mgs1
    End Sub
    
    Sub test2()
        msg1 = "message"
        msg2 = "another"
    
    
        MsgBox msg2 & " " & msgl
    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

  5. #5
    Thank you Paul and snb for the input. As you said Paul, "If it's working don't mess with it". I think I will stick with that approach with this one. If I start to find errors I will deal with them as they come. But for now everything is working. I just wanted to get someone else's viewpoint. I appreciate the input.
    -David

Tags for this Thread

Posting Permissions

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