+ Reply to Thread
Results 1 to 8 of 8

How do I assign a global constant variable to the current year date?

  1. #1
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Question How do I assign a global constant variable to the current year date?

    I am trying to setup a global constant variable for the current year using the date function, but I get an error" Compile Error - Constant Expression required". I am sure that must be in the way I am trying to do it. I want to be able to use the variable "CYear" in all the coding for my workbook instead of having to use the "Format(Date, "YYYY"). I have tried using the "Today(Now) formatting also without success. Here is what I have in Module1 with all of my other declarations:

    Please Login or Register  to view this content.
    I have the "Option Explicit" off and all help is appreciated.

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

    Re: How do I assign a global constant variable to the current year date?

    The answer is on the error message-Constant Expression required".
    You can't reassign a constant. You need to define it and assign a const value in one go.

  3. #3
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: How do I assign a global constant variable to the current year date?

    I am new to VBA and don't understand what you mean. I thought that is what I was doing. Where am I reassigning CYear? I am assigning CYear as a string with a value of the current year. I will be making new worksheets and workbooks using the current year as the year changes so I need a global constant variable. Please explain where I am reassigning the variable CYear or explain how I may assign the current year as a global constant. Thanks in advance for your help.

  4. #4
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: How do I assign a global constant variable to the current year date?

    Or explained another way, a constant is something that doesnt change. A variable (as per its name) is something that does change.

    So if you try and assign a constant a value that could change automatically (like a function such as date or now) it fails because it knows that its not a static value.

    You may be able to do something like this instead (declaring and assigning value to a variable at the same time):

    Please Login or Register  to view this content.
    However, what was your reasoning behind wanting to do this (I am not judging, I have tried this very thing before)?

    I know for myself, I tend to put constants towards the top of my code as a way to reuse values multiple times in my code in such a way if I need to change it I can do so in 1 place (the constant) instead of each place in my code I use it. I also tend to use constants for values I will use more than once and despite not needing to ever change them I know if will speed up code (ex: 1, 2, 0, "A1", etc). The last reason I use constants, when writing code for others, is to give them an easy way to change a value used in the code.

    Its that last one that led me to what you have tried. I often find in this case its better to provide a way for the user to input the desired parameter and store it in a variable for use in the code. It can be something as simple as a cell on a sheet they input into or a message/input box. It can be more advanced like a custom form to gather information and validate the entry before running.

    So long story short, you can do it as a variable instead or you may consider gathering that piece of input some other way.
    Ways to get help: Post clear questions, explain your overall goal, supply as much background as possible, respond to questions asked of you by those trying to help and post sample(s) files.

    "I am here to help, not do it for people" -Me

  5. #5
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: How do I assign a global constant variable to the current year date?

    So based on one of your responses it seems like you may not be aware of the difference between a variable and constant and how they are declared and assigned values. Depending on how serious you are about learning VBA you may consider reading up on this fundamental concept. You may also want to look into scope as well.

    The short of it is as follows (my apologies if I have underestimated your understanding of VBA):

    A variable is a name we give to a value that can be anything, it can even change in the course of running our code. A variable will be of a certain data type which dictates the type of data (or object) it can store and how much memory it uses to store said data.

    The typical method of doing this is:

    Please Login or Register  to view this content.
    In which "variablename" is whatever you want to call your variable and "type" is the data type, like "String" or "Integer" (without quotes).

    A constant is like a variable, in that it is a name we give to a value to use later in our code by its name. The difference is a constant is assigned a static value at the time of declaration and it remains the same value. It also is declared with a data type.

    Please Login or Register  to view this content.
    In which "constantname" is whatever you want to name the constant and "type" is the data type, like "String" or "Integer" (without quotes).

    When dealing with scope, the syntax to declare changes slightly but all the rules still apply.

    Variable (Public scope)
    Please Login or Register  to view this content.
    Constant (Public scope)
    Please Login or Register  to view this content.
    You should only declare variables beyond the default scope when absolutely needed.

  6. #6
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: How do I assign a global constant variable to the current year date?

    Okay, I think I got it. What I was trying to do was as you said "make it easy or the user (my coworkers). They are not much on computers and I was trying to automate this workbook so they did not have to be "techs" and to make using this easy for them. At the end of the year a new workbook will be created and maintained as the current year information. I am trying to automate the process for them so all they need to do for a new year is click a button that closes the old workbook and creates the new year workbook. I was not using the correct verbiage, I want to assign CYear to be the current year as a string name to use in the code but at year end change it's value to the next year as the year changes. The workbook name would be "WorkbookName_2018" (CYear) for this year with worksheet names "worksheet_A_2018" (CYear), "worksheet_B_2018" (CYear),etc. So I want to use a constant name "CYear" so when the new year starts all code that references the year is updated from the "CYear" Name if that makes sense.

    I tried your suggestion of:
    Please Login or Register  to view this content.
    But it returns an error of "Compile error Expected: End of Statement" with the "=" highlited. Do you have any ideas why?

  7. #7
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: How do I assign a global constant variable to the current year date?

    Its possible you may not be able to assign the value during declaration with public scope variables.

    As I mentioned there are better alternatives to this. The easiest would be a cell on the sheet with data validation. Make the validation a whole number from whatever start year you like to whatever future year you like, ex: 2015 to 2050. In this fashion you can prevent other entries, provide a dialog warning should they try an improper entry and ensure the data type is a 4 digit numeric value. Then in your code use a range variable to store the cell itself, and you can declare another variable to store its value (to later concatenate to your file name). You may have to play with the data type of the second variable (either converting it to string during to storing it or converting it when concatenating to the name). This will make it very easy for them to change, very easy for you to code, and very "strict" in terms of what they can enter which is good (as a programmer you cannot always assume the user will enter information properly).

    As a twist on the above, you could put in a cell the following formula if you want it to be current year (like your OP code), then just skip the validation and reference the cell...maybe consider locking the cell to prevent someone replacing the formula with manually entered values.:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Now that I think about it, you could also just do:

    Please Login or Register  to view this content.
    You can simply put a comment above it to explain what they need to change (ie: 2018 to any valid 4 digit year)

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: How do I assign a global constant variable to the current year date?

    COURTTROOPER,

    Constant is Constant. That's all.

    You can not assign the value from function/other variable, it must be constant.

    One way is to declare global variable and initialize it in other sub procedure like
    In a standard module
    Please Login or Register  to view this content.
    And in ThisWorkbook code module
    Please Login or Register  to view this content.
    Then you can call CYear from the sub/function procedure. like
    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. Assign a global variable to Excel Active X textbox
    By daustin5 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2016, 08:52 PM
  2. VBA Global Constant
    By zhbuyi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2016, 04:28 PM
  3. [SOLVED] Set PivotTable Filter to Current Day, Current Week, Current Month, or Current Year
    By EnigmaMatter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2014, 08:31 AM
  4. [SOLVED] how do I assign current selected cells to variable ?
    By dschmitt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-06-2014, 09:39 PM
  5. Replies: 3
    Last Post: 09-04-2013, 10:49 AM
  6. [SOLVED] Keeping a date as current year, even though formula determines previous year
    By dropanddrive03 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-28-2013, 08:31 PM
  7. Assign current cell to a variable and re-select
    By Cumberland in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-29-2006, 10:47 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