+ Reply to Thread
Results 1 to 4 of 4

Scoping Public Variables Across Workbooks

  1. #1
    Registered User
    Join Date
    09-18-2012
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    24

    Scoping Public Variables Across Workbooks

    Hi

    I have a problem with defining variables.

    In have a workbook called mybook1.xlsm. At the top of a code module within that workbook I have a public variable declared

    Public kdatabase as string

    I can use that quite happily in any code modules within mybook1.xlsm.

    But if I refer to that variable in a sub that is in my personal.xlsb then i get an error

    For example i haver this sub in a code module from the perosnal.xlsb

    Sub AddDb
    kx=kdatabase
    ....
    End Sub

    This generates an error when AddDb when it is called from a sub in mybooks1.xlsm.

    Does this mean a public variable is only available to subs from within the same workbook?

    If that is not the case how do I define my variable so that it can been seen from both workbooks?

    Thanks

    Max

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Scoping Public Variables Across Workbooks

    AFAIAA variables are limited to the workbook/project in which they are declared.

    Have you considered writing the value of a variable to a specific cell in one workbook and then addressing / reading that variable from another workbook by fully addressing the workbook/sheet/cell?
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,820

    Re: Scoping Public Variables Across Workbooks

    If I read this (http://www.cpearson.com/Excel/Scope.aspx ) and this (http://www.cpearson.com/Excel/References.htm ) correctly, you can make variables and procedures in one VBA project available to another project by setting a reference to the project that contains the desired variable/procedure.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    09-18-2012
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    24

    Re: Scoping Public Variables Across Workbooks

    Thank you for your replies.

    I tried defining the variable but couldn't get anywhere with that. So I have used your idea Richard and save my variable in a worksheet within Personal.xlsb.

    I'm not sure if this is recommended practice but it works! So hey ho.

    Max

+ 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. Public variables and how to use them
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 11-21-2018, 03:58 PM
  2. [SOLVED] public variable scoping
    By keithwins in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2015, 12:59 PM
  3. [SOLVED] Public Variables
    By iamGreenhorn in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-21-2014, 12:38 PM
  4. Public variables
    By Tomski in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-10-2006, 10:20 AM
  5. [SOLVED] Public Variables
    By [email protected] in forum Excel General
    Replies: 6
    Last Post: 09-12-2005, 12:05 PM
  6. [SOLVED] Public Variables
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2005, 12:05 PM
  7. Public variables
    By jcp29 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-02-2005, 07: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