+ Reply to Thread
Results 1 to 5 of 5

Do I need "Global" or is there an alternative?

  1. #1
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Do I need "Global" or is there an alternative?

    I'm trying to use 3 input values and 1 derived (vlookup) value in 4 cells on a worksheet To open , close, save as and input data on several other workbooks. I can do this, but have to Dim and Set values for every Sub which uses the data. I've confused myself researching Global, Public & Constant and wonder if I'm overthinking things. Sub "Wanted" shows what I want to achieve in (hopefully) plain language. Then subs in other modules need to access the "Public" line. Hope this is clear. Sub "Got" shows what I'm using.

    Cell data examples - Fle; 14/1/17; 2017; Flem. Normally change each time code runs

    Any help appreciated.

    Please Login or Register  to view this content.
    Last edited by swallis; 04-21-2017 at 06:43 PM.
    Steve W.
    Vba is my hobby, racing the means.

  2. #2
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Do I need "Global" or is there an alternative?

    Can't believe I've done this! The following code works!!!!!

    It seems I have to call "SetAll" each time I want to access the Public Variables. Is this right?

    Also I need some help with changing the format of the date taken from F2. It is dd/mm/yyyy in the cell, but I need to use it as ddmmyyy for cell2 and yyyymmdd for cell5. Any help on that please?

    Would appreciate any thoughts on the original question and my solution, but will thank you for your assistance tomorrow. I've been working on this all day, I'm brain dead and need a drink.

    Cheers

    Steve

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Do I need "Global" or is there an alternative?

    There is no "Global variable", but concept only. Global is in sense means accessible from any where in a project. So, if you declare a variable as public, out side a module( On top of a module), it becomes a global variable, but you use public variable name to declare it. This variable can be seen by any module in a project.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Do I need "Global" or is there an alternative?

    Quote Originally Posted by swallis View Post
    It seems I have to call "SetAll" each time I want to access the Public Variables. Is this right?
    You wouldn't necessarily have to call SetAll each time. Call SetAll once to initialize the variable values and then anytime the cell values change so as to update the variables. That may or may not mean every time for you depending on how often the cell values change. The values in the public variables persist in memory, so you only need to update them when the cell values change.



    Also I need some help with changing the format of the date taken from F2. It is dd/mm/yyyy in the cell, but I need to use it as ddmmyyy for cell2 and yyyymmdd for cell5. Any help on that please?
    Please Login or Register  to view this content.
    Alternatively, you could just read the date into one Date-type variable. Then in the code, format the date any way you like when you use it e.g.; Format(MyDate, "ddmmyyy")

    Please Login or Register  to view this content.
    One tip: in your original code, you declare a variable called Year. That is also a keyword used in VBA. While it is legal to have variable names the same as VBA keywords, it is not good coding practice. It can be confusing to read the code and, once in a while, it will cause vba to puke. I know you said you've read a lot, but here's one more you should read.
    Constant and Variable Naming Conventions
    Last edited by AlphaFrog; 04-22-2017 at 01:34 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Forum Contributor
    Join Date
    04-07-2009
    Location
    Tasmania, Australia
    MS-Off Ver
    Excel 2003
    Posts
    158

    Re: Do I need "Global" or is there an alternative?

    Thanks for the replies.

    Alphafrog:
    You wouldn't necessarily have to call SetAll each time.
    I must have been really brain dead last night. Kept getting error messages when I tried to run the code again without "set". Today it works fine. Process will be that each time I change any of the cells they will be used for the entire Project, so I just have to make sure that the first Sub calls Setall?

    I like your first solution to the format problem. I nearly had it, but had the .value at the end of the line.

    One tip: in your original code, you declare a variable called Year.
    Yes. All my names are temporary while I get things to work. I was intending to Rename them and put a P in front. So thanks for the link, I see it advise against that and suggests G instead.

    Thanks for your help. You've set me up for a lot of fun next week.

    regards

    steve

+ 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. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  2. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  3. Error "Range of object Global failed"
    By AdiK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-27-2016, 02:01 PM
  4. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  5. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  6. Replies: 1
    Last Post: 05-23-2006, 01:40 PM
  7. [SOLVED] Urgently need VB Help - Declaring "Global" Variable?
    By April in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-18-2005, 10: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