+ Reply to Thread
Results 1 to 14 of 14

Insert Formula with Variable Range

  1. #1
    Registered User
    Join Date
    07-14-2014
    Location
    Los Angles, United States
    MS-Off Ver
    2007
    Posts
    12

    Insert Formula with Variable Range

    I'm trying to input a simple formula via VBA to sum data which has a variable range. I need something like =sum(variable range).

    Column A Column B
    1 Example 1 15
    2 Example 2 20
    3 Example 3 12
    4 Example 4 5
    5 Total: =sum(B1:B4)

    This is what I am looking for; but my problem is that range B1:B4 might actual be B1:B10 depending on which project I'm in.

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Insert Formula with Variable Range

    Duplicate post.
    Click the * Add Reputation button in the lower left hand corner of this post to say thanks.

    Don't forget to mark this thread SOLVED by going to the "Thread Tools" drop down list above your first post and choosing solved.

  3. #3
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: Insert Formula with Variable Range

    Try this:
    Please Login or Register  to view this content.
    If this has been helpful, please click on the star at the left.

  4. #4
    Registered User
    Join Date
    07-14-2014
    Location
    Los Angles, United States
    MS-Off Ver
    2007
    Posts
    12

    Re: Insert Formula with Variable Range

    I apologize for the duplicate threads. For some reason, my posts are not showing up under "My Posts".

  5. #5
    Registered User
    Join Date
    07-14-2014
    Location
    Los Angles, United States
    MS-Off Ver
    2007
    Posts
    12

    Re: Insert Formula with Variable Range

    StuCram, Thank you. That did work; however, what if I have multiple groups of areas which need to be summed in that same column? For example, I have a group of data from B1:B4 which needs to be summed. Then I have a group of data from B6:B18 which needs to be added. My spreadsheet has several groups of data which need to be summed. And each group can change with the amount of rows it contains. I hope this makes sense.

  6. #6
    Registered User
    Join Date
    07-14-2014
    Location
    Los Angles, United States
    MS-Off Ver
    2007
    Posts
    12

    Re: Insert Formula with Variable Range

    I've been trying the following, but it's giving me an error saying "Application-defined or object-defined error". If I could get this code to work, I would simply write the code to navigate to the cell where I want the "ActiveCell" to be and then let it work from there.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Insert Formula with Variable Range

    Maybe this.
    You can see it work in the workbook at the bottom of this post.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    07-14-2014
    Location
    Los Angles, United States
    MS-Off Ver
    2007
    Posts
    12

    Re: Insert Formula with Variable Range

    I finally worked one out on my own!! This is what I have and it appears to work great so far.

    Please Login or Register  to view this content.

  9. #9
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Insert Formula with Variable Range

    Quote Originally Posted by excel6000 View Post
    I finally worked one out on my own!! This is what I have and it appears to work great so far.
    I don't see how this solves your post #5 concern.

  10. #10
    Registered User
    Join Date
    07-14-2014
    Location
    Los Angles, United States
    MS-Off Ver
    2007
    Posts
    12

    Re: Insert Formula with Variable Range

    I actually moved my data to column C. So basically I use code to navigate to the cell where I want the formula, then I run the code which I put in Post #8 and it will sum up everything for that particular group of data. Then I used code again to navigate to the next cell where I need subtotals. What I ended up doing was navigating to the cell, then using the code from post #8, and then using a loop to run through it again until it gets to the bottom of the data.

  11. #11
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Insert Formula with Variable Range

    Well why aren't you sharing the code? This is a learning site. If you have code that works it's customary to share the solution for the benefit of those coming here with a similar need or just those that want to learn how to code.

  12. #12
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: Insert Formula with Variable Range

    To excel6000;

    At first you asked to get a formula to add up a set of numbers in column B and that got answered.

    Now, you've changed the problem in the same thread to get formulas for each of several groups of numbers in column B; perhaps that should have been a separate thread. Just a thought for future postings and questions.

    Anyway, here's a solution to do that.
    - Whenever a group of numbers in column B is followed by a blank cell,
    a formula is inserted in that blank cell to get total of the numbers.
    - additional blank cells are ignored.
    - The total is formatted as red bold text for easy reference.

    Hope this helps. See the attached file Make formulas for subtotals.xlsmfor a sample set of data and a button to run the macro.
    Please Login or Register  to view this content.
    Last edited by StuCram; 12-10-2015 at 02:50 AM.

  13. #13
    Registered User
    Join Date
    07-14-2014
    Location
    Los Angles, United States
    MS-Off Ver
    2007
    Posts
    12

    Re: Insert Formula with Variable Range

    StuCram, you are correct, I should have started a new thread. I apologize for that. Thank you very much for your help!

    Skywriter, below is the rest of my code which you asked for. Please also see image below (or actual excel file which is attached) to see what I was trying to accomplish. Sorry for all of the confusion.


    Please Login or Register  to view this content.
    Capture.JPG
    Attached Files Attached Files

  14. #14
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    2,760

    Re: Insert Formula with Variable Range

    I'm not sure why you didn't see how well the code I posted in post #7 would work for your needs.
    Here's a modified version to fit the file you posted.
    There's a working file below the code.

    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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] insert formula with variable
    By jed38 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-31-2013, 02:54 PM
  2. [SOLVED] Insert Rank formula into variable range
    By on top in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2013, 04:01 PM
  3. [SOLVED] Macro to insert COUNTIF with a variable range
    By Jim.Kelly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-20-2013, 04:07 AM
  4. [SOLVED] What is the proper syntax to insert a variable as a the row # of a range
    By OliveMarie in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-24-2012, 01:55 AM
  5. Insert variable in formula
    By Len Silva in forum Excel General
    Replies: 2
    Last Post: 11-03-2009, 05:11 PM
  6. insert a variable into a formula
    By pappy in forum Excel General
    Replies: 1
    Last Post: 08-04-2006, 08:50 AM
  7. variable - insert blank row/select range
    By Jan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-13-2006, 12:45 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