+ Reply to Thread
Results 1 to 5 of 5

Is it possible to define variable once rather than in each sub

  1. #1
    Registered User
    Join Date
    02-24-2019
    Location
    UK
    MS-Off Ver
    MS 365 PC & Mac (assume PC unless otherwise stated in post)
    Posts
    96

    Is it possible to define variable once rather than in each sub

    I am relatively new to world of vba, but have been working with it quite intensively over recent months learning as I go.

    I recently discovered that I don't need to dim my objects and variables at the beginning of each sub and can do this once at the top of the module or form code. I wanted to extend this further and set regular variables once per module (or even better once for whole workbook (if possible).

    Below is example of a variable I am regularly using (pmtName) and I show the context I am using it in (Msg box Title), at the moment I define this in each sub exactly the same, but if I could just do this once or once per module/form that would be big improvement.

    Any guidance would be greatly appreciated.

    Please Login or Register  to view this content.

  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: Is it possible to define variable once rather than in each sub

    You could declare the variables as Public in a standard module but you would need to be careful when using them especially if the values will change across modules.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-24-2019
    Location
    UK
    MS-Off Ver
    MS 365 PC & Mac (assume PC unless otherwise stated in post)
    Posts
    96

    Re: Is it possible to define variable once rather than in each sub

    Thanks Norie, so would that look something like this on a Module;

    Please Login or Register  to view this content.

  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: Is it possible to define variable once rather than in each sub

    No, you can't set the values like that - you would still need to set the variables values inside Subs/Functions.

  5. #5
    Registered User
    Join Date
    02-24-2019
    Location
    UK
    MS-Off Ver
    MS 365 PC & Mac (assume PC unless otherwise stated in post)
    Posts
    96

    Re: Is it possible to define variable once rather than in each sub

    Ok I understand, thanks. That answers my question that it is not possible to set the values once.

+ 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. Problem: How to define my variable?
    By APV in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-06-2015, 07:56 AM
  2. How to use a Define Name variable in VBA procedure
    By davebliz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-12-2014, 05:13 AM
  3. [SOLVED] how to define a variable?
    By inf3 in forum Excel General
    Replies: 5
    Last Post: 05-09-2014, 05:49 PM
  4. using a variable workbook's name to define another variable
    By lordterrin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-30-2013, 05:57 PM
  5. VBA Define Variable
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-18-2012, 12:55 PM
  6. How to define a variable range and use it.
    By pharaz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-08-2012, 12:14 PM
  7. How to define variable
    By kishore in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-13-2005, 06:06 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