+ Reply to Thread
Results 1 to 7 of 7

Declaring Variables Across all Sub Procedures

  1. #1
    Registered User
    Join Date
    07-29-2013
    Location
    California, United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Declaring Variables Across all Sub Procedures

    I am using Microsoft 2010. I am new to Excel Macro VBA. I am still confused about the differences between Sub, Public, Global, Function, Dim, and Set. No idea when to use which.

    Currently, I have a lot of worksheets and cells that I want to give a variable or name to.

    This is not the actual code, but just an example of what I am trying to do.

    Sub Example()

    'Define Worksheets

    Set sheet1 = Worksheets("Red")
    Set sheet2 = Worksheets("Blue")

    'Define Cells

    Set var1 = sheet1.Cells(1,1)
    Set var2 = sheet2.Cells(2,2)


    'A long code of procedures follow using this variables.

    End Sub


    Now, I have another set of procedures where I want to use those same variables without having to define them again.

    For example,

    Sub Example2()

    sheet1.Cells(3,3) = var1+var2

    End Sub


    I know I am supposed to use "Public" or something, but I keep getting errors or my code doesn't run. The only way it runs is if I redefine those variables under each new procedure "Sub ____()."

    Also, I want to define these variables, such that I also don't need to re-define them under "Private Sub" (whatever that is).

    Also, how do I set up this forum, so I am not viewing my message via HTML format?

    Thanks a plenty!
    Last edited by bananajelly; 07-29-2013 at 01:19 PM.

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

    Re: Declaring Variables Across all Sub Procedures

    Why don't you want to redefine the variables?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    07-29-2013
    Location
    California, United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Declaring Variables Across all Sub Procedures

    Because I have a lot of variables and Sub procedures. I have 10 different variables that I re-use in each new procedure. I don't want to redefine them every time. If I want to edit a variable, then I have to change the code under every procedure.

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

    Re: Declaring Variables Across all Sub Procedures

    Why would you need to rewrite code?

  5. #5
    Registered User
    Join Date
    07-29-2013
    Location
    California, United States
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Declaring Variables Across all Sub Procedures

    Norie, I can't tell if you're being a troll or not.

    I don't need to rewrite the code, but I would need to copy/paste all my variable statements under each Sub Procedure. Do you not understand my question?

    In order for Sub Example2() to work, I would need to copy/paste the definitions again. Otherwise, var1, var2, sheet1, and sheet2 will not be recognized in the new procedure. I need to define these variables across ALL procedures, so I don't have to keep copy/pasting the same lines under every new procedure.

    Sub Example()

    'Define Worksheets

    Set sheet1 = Worksheets("Red")
    Set sheet2 = Worksheets("Blue")

    'Define Cells

    Set var1 = sheet1.Cells(1,1)
    Set var2 = sheet2.Cells(2,2)

    'A long code of procedures follow using this variables.

    End Sub



    For new procedure, I would need to redefine all the variables:


    Sub Example2()


    'Define Worksheets

    Set sheet1 = Worksheets("Red")
    Set sheet2 = Worksheets("Blue")

    'Define Cells

    Set var1 = sheet1.Cells(1,1)
    Set var2 = sheet2.Cells(2,2)

    sheet1.Cells(3,3) = var1+var2

    End Sub


    VERSUS just

    Sub Example2()

    sheet1.Cells(3,3) = var1+var2

    End Sub
    Last edited by bananajelly; 07-29-2013 at 05:28 PM.

  6. #6
    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: Declaring Variables Across all Sub Procedures

    Please Login or Register  to view this content.
    You need a really good reason to do this, though -- inappropriate use of public variables makes debugging difficult, code delicate, and well-structured design impossible.
    Entia non sunt multiplicanda sine necessitate

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

    Re: Declaring Variables Across all Sub Procedures

    Troll?

    I asked a legitimate question.

    You said you had to rewrite your code, which I assumed meant rewriting existing code not adding to existing code.

    If you really want to use public variables, which can be a bad idea, then declare them as public in a standard module outwith any other subs.

    There might be different ways to do this, something that will keep tighter control on the variables.

    For example you could pass the sheets/ranges etc as arguments to the subs.
    Please Login or Register  to view this content.

+ 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. [SOLVED] declaring variables help
    By jakeembx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2013, 04:03 PM
  2. Declaring Variables - Why?
    By The Drone in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-02-2012, 09:01 AM
  3. Declaring Variables
    By Weston33 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2012, 05:39 AM
  4. Declaring of variables
    By pspyve in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-08-2011, 08:44 PM
  5. Declaring variables
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-28-2005, 03:10 PM

Tags for this Thread

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