+ Reply to Thread
Results 1 to 10 of 10

Use a variable in a formula in VBA

  1. #1
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Use a variable in a formula in VBA

    Evening, Gurus.

    This will be an easy one for you guys, but try as I migh I can't get the syntax right.

    I'm simply trying to usea the variable "AmtEnd" to specify the end of the range I am trying to sum. The first value is constant, "O7", but the last row varies, so I dime the variable as a range, and then set the variable, and am trying to use that variable as the end of my range to sum. I am getting a "#VALUE" error in teh cell instead of a sum. It is the last line of this script throwing the error:

    Please Login or Register  to view this content.
    Thanks in advance for any help you can offer.
    Last edited by [email protected]; 04-19-2018 at 07:59 AM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Use a variable in a formula in VBA

    Try this one.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 04-18-2018 at 05:21 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    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: Use a variable in a formula in VBA

    Is it vital to put a total at the foot of a column of numbers?

    That was standard when we used pencil and paper but ever since spreadsheets were created it's always been easier to hold a total in a fixed cell above the data.
    Either create a sum formula that includes as many rows as your data is ever likely to occupy, or create a dynamic range name for your data and use that.

    There's also a lot of .Select and Activate stuff which you should try to avoid. It's rarely necessary.
    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.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Use a variable in a formula in VBA

    You don’t need to calculate it anyway as it’s just the cell above, so
    Please Login or Register  to view this content.
    which sums from row 7 of the current column to the cell above the one with the formula.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Use a variable in a formula in VBA

    Morning, guys. Sorry for my slow reply. I cut out of the office right after my post yesterday.

    xlnitwit - I see what you are saying, but the cell where my formula will be is not always the same. There are varying rows of data that will have to be summed, so that is why I was trying it using a variable.

    Richard - Good to hear from you again. It has been awhile. You make a very good point. It would have been easier to select a constant cell address above the data and then simply select a range that would consistently encompass all the rows that would ever be used. I could then just reference that cell to retrieve the sum. Seems like I'm always doing things the hard way. I may still go with your example.

    Bakerman2 - You nailed it. In my many attempts at changing the syntax of the bottom line I had tried the version you used, but the problem was that I had tried to dim my variable as a range rather than a string. I still don't completely understand why it is a string, since it was a cell address I was trying to set as a variable, but it seems to work.

    Thanks to all of you for you help and expertise. I am always amazed by the helpful people involved in this forum. I will mark this thread as SOLVED. Have a good one.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Use a variable in a formula in VBA

    Is the formula cell not always immediately below the data then?

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Use a variable in a formula in VBA

    Glad to help. Thanks for rep+.

  8. #8
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Use a variable in a formula in VBA

    The formula cell is always right below the data, but the number of rows of data is dynamic. I decided to go with Richard's suggestion to put the formula in a static location and reference a static range like "(O7:O50000)". This eliminated the need to use a variable.

    Thanks.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Use a variable in a formula in VBA

    If the data starts at row 7, then the formula I suggested will always work. No variable required, no excessive ranges.

  10. #10
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Re: Use a variable in a formula in VBA

    I got you now. I had already selected the cell below the data. You're right that will work, too. Thanks.

+ 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. Changing column variable on a copied formula instead of row variable
    By dsw283 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-06-2014, 04:13 PM
  2. Help with a 3 variable formula
    By Tweedy86 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-26-2013, 07:54 AM
  3. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  4. [SOLVED] VBA Variable in Sum formula
    By DHuskic in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-17-2013, 01:55 PM
  5. [SOLVED] Formula using Variable Row
    By jacob@thepenpoint in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2013, 06:27 PM
  6. Replies: 1
    Last Post: 10-31-2006, 08:34 AM
  7. Formula in variable
    By Kaziglu Bey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-05-2006, 08:23 PM

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