+ Reply to Thread
Results 1 to 6 of 6

Creating a macro with variables

  1. #1
    Registered User
    Join Date
    09-04-2014
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    52

    Question Creating a macro with variables

    Hi all, I currently have a macro that updates/saves a number of different data sets for a specific period. I need to be able to make it be able to update the data sets for various different periods.. how would I go about doing this? Appreciate any help whatsoever! Thank you in advance for any tips!

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Creating a macro with variables

    Please take the time to read the forumrules, especialy the one's about crossposting

    http://www.mrexcel.com/forum/excel-q...variables.html
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Registered User
    Join Date
    09-04-2014
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    52

    Re: Creating a macro with variables

    Thank you, I've looked through but I'm very new to macro and not exactly sure how id go about doing this. My current macro is below. It updates data for different variables for the period 2014 quarter 4. I want it to be able to be flexible so for example I can type in 2015 q3 and test 5 (different folder to save this data in) and it will change all areas where it says 2014 q1 and the word test. Do you suggest creating a macro to do this or a master list.. And how would I go about doing this? thanks!!

    Please Login or Register  to view this content.

  4. #4
    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: Creating a macro with variables

    Hi,

    Create and name some variable cells on a sheet that will hold your values 2014, q4, test etc. Name them say 'year', 'qtr', 'text'

    Then in your macro since you'll use them several time Dim some variables to hold them.
    e.g.

    Please Login or Register  to view this content.
    Now in your code use these variables. e.g.

    Please Login or Register  to view this content.
    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.

  5. #5
    Registered User
    Join Date
    09-04-2014
    Location
    Sydney, Australia
    MS-Off Ver
    2013
    Posts
    52

    Re: Creating a macro with variables

    That definitely looks like the type of function I'm after.

    Can the sheet with the variable cells be on a new workbook? And how do I link this sheet with the macro?

    I have an error coming up in the line
    Please Login or Register  to view this content.
    I have a feeling its because I haven't saved the sheet with variable cells correctly..

  6. #6
    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: Creating a macro with variables

    Hi,

    Yes, the variable cells may be on a completely different workbook. Obviously when you read the variable cells, instead of just using the range name (which by default is assumed to be in the workbook containing the macro you're running) you'll need to fully qualify this. e.g.

    Please Login or Register  to view this content.
    Did you Dim the variable as lYear as per my suggestion or just 'Year'? If so you need to use

    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] Creating a look up table for X and Y variables
    By ericfoster75 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-21-2014, 02:00 AM
  2. Help with creating a graph with three variables
    By Grish in forum Excel General
    Replies: 0
    Last Post: 08-21-2012, 11:04 AM
  3. Creating For Loop with 3 variables
    By BeefNoodleSoup in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-16-2012, 05:25 PM
  4. macro that finds variables in one column and enters values if variables found
    By xln00b in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-07-2010, 08:22 AM
  5. Creating a formula that will SUM with 2 Variables
    By B Akers in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-28-2006, 02:10 AM

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