+ Reply to Thread
Results 1 to 17 of 17

VBA Insert Formula to SumIf with Variable Sheet Names

  1. #1
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    VBA Insert Formula to SumIf with Variable Sheet Names

    This might be my biggest challenge yet (granted I'm new to VBA). In the attached file I have a Capital sheet. This is where my managers can see their current asset costs and put any upcoming purchases. I need this data to feed into my cost center sheets. I need to be able to insert a formula in the cost center sheet that will be updated with any changes made in the Capital sheet. The challenge is...there will be multiple Capital sheets in the file as there are multiple cost center sheets in the file. I've played around with a SumIf formula in Excel that I can get to pull the data using a variable

    Please Login or Register  to view this content.
    but I need to be able to have the sheet name as a variable too. I have been trying to write a macro that can say If the first five of the cost center sheet name = the last right of the Capital name then the SumIf formula would be populated with the matching Capital sheet name. I would really appreciate any help that you could give.

    Thank y
    Attached Files Attached Files

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    This formula will reference the Capital sheet with the ID number from cell A1

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

  3. #3
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    When I try to use VBA to populate a cell with that code I get an Application-defined or object-defined error. Can you please help me figure out what is wrong (I tried using .value and .FormulaR1C1)?

    Thank you,

    Please Login or Register  to view this content.

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    The one literal quote character " within the formula are represented by two quotes each ""

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    AlphaFrog,

    When the cell is populated by the formula I get a REF error.

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    Quote Originally Posted by PandoraGirl View Post
    AlphaFrog,

    When the cell is populated by the formula I get a REF error.
    it worked for me using your example workbook.

  7. #7
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    AlphaFrog,

    I figured out what I was doing wrong (the name of the worksheet was slightly different in my real file). I was able to correct that and the formula works great! The only challenge I have is that, as you can see in the sample file, I have many lines and many months to pull data from. I ran the macro putting in the ranges but it inserts the exact same formula in each cell so it is always pulling from the Capital sheet column I. What I need is for it to pull from column I, then J, then K and so on (all cells shaded in light gray from Aug-19 on will need to have their own formula). I know I can write the code for each individual cell but that is going to be a lot of code (my master file goes until 12/2022). Do you have an idea of how to streamline this?

    Thank you!
    Last edited by PandoraGirl; 07-24-2019 at 05:40 PM.

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    AlphaFrog,

    Can you please help me understand parts of this code? When it populates the cell I see the following:

    Please Login or Register  to view this content.
    I know what the C2 and C9 are referring to. I don't understand the RC1 although I think it has to do with taking the left six digits of Column A (account number). Although this macro populates the cells with the code above (changing in each cell), it does not appear to be working quite right because I am getting zeros instead of the numbers from the Capital sheet.

    Thank you once again for your time and efforts to help!

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    The line of code should have been...

    Please Login or Register  to view this content.
    The cell references within the formula are in the R1C1 reference style
    C2 is Column 2 or column B
    RC1 is current row column A
    C9 is column I

    LEFT(RC1,6) is suppose to extract the six digit code from column A in the same row the formula is in.

    The VBA code worked for me in your example workbook

  11. #11
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    AlphaFrog,

    Thank you so much for all of your help! This worked great! I do have one more question...I am modifying this code to work with another worksheet that will need to populate the Master sheet column I (9) with the data in column K (11) on the Maintenance sheet. How do I do that with this formula? I have tried changing a variety of things but it still refers to column 9. Thank you once again!
    Last edited by PandoraGirl; 08-01-2019 at 03:15 PM.

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    Quote Originally Posted by PandoraGirl View Post
    populate the Master sheet column I (9) with the data in column K (11) on the Maintenance sheet.
    Too vague. Don't know what that means.

  13. #13
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    AlphaFrog,

    I am using the same formula you created above to populate my cost center sheet with data from my Maintenance sheet. The Maintenance sheet looks very similar to the Capital sheet except the data that I need starts in column K (Aug) instead of column I like it did in the Capital sheet. So, the cost center sheet column I needs to bring in the data from the Maintenance sheet column K and so on through the end of 2022. I have updated my file to better explain what I am talking about. This is the code I am currently using but it is bringing column I to I.

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

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    I think you just want to offset from the formula column to the data column. So if the formula is in column I, then offset 2 columns to the data column K

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    AlphaFrog,

    Thank you so much for all of your help! All of this works great!!! I ran across one issue during final testing today. Not all cost center tabs seem to have a Capital or Maintenance tab to populate from. How would I tell the macro if a sheet doesn't exist to skip inserting the formulas? I've tried various things but it tends to skip doing anything.

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

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    11-20-2017
    Location
    Harrisburg, PA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    56

    Re: VBA Insert Formula to SumIf with Variable Sheet Names

    AlphaFrog,

    This is great and the last piece I needed to finish up this part of my project! The research I did online did not even mention Evaluate. I was trying to use SheetsExist and that was not obviously working. Thank you again for everything!

+ 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 names to master sheet present to other worksheets (only one time)
    By sanpoli in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 5
    Last Post: 08-09-2017, 08:39 AM
  2. [SOLVED] Activate variable INTERNAL sheet names
    By rpinxt in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 03-14-2016, 03:39 AM
  3. Insert a vlookup with variable array from another sheet.
    By Chris S. in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-07-2013, 02:32 PM
  4. Macro to insert variable formula depending on sheet name
    By Burt_100 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-26-2013, 03:22 PM
  5. Variable sheet names
    By eran3185 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-21-2010, 09:59 AM
  6. Referencing variable sheet names in formulas?
    By Alabaster in forum Excel General
    Replies: 1
    Last Post: 04-24-2010, 11:20 AM
  7. Using variable names to select sheet names
    By mattsgr1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-13-2007, 06:38 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