VBA Add Formula to varying cell reference

SpicyItalian

New Member
Joined
Feb 24, 2023
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
The premise of my project is that based on user inputs in a tab (Tab1), it will auto calculate the required materials for a PO on a different tab (Tab2). Tab2 has every potential line item that could be ordered. Once the inputs are in place they are multiplied by the cost of each individual unit in a cell to the right.

I have a button that when pressed it deletes all rows that have 0 quantity balances. It then copies the sheet, creates a new workbook, and then pastespecials as values.
The issue I need help with is having a formula generate underneath the sheet after it is pasted as values. The reason i cannot figure this out is because that "sum cell" will be different every time based on the required materials. Is there a way to determine which cell would be the proper one to generate this that would be variable anytime. The cell to the left of my desired "sum cell" has the text "Total". Not sure if this could help to generate some sort of anchor point for this.

Any and all help is appreciated!!
 

Attachments

  • RQ1.png
    RQ1.png
    202.5 KB · Views: 28
  • RQ3.png
    RQ3.png
    33.6 KB · Views: 33
  • RQ4.png
    RQ4.png
    64.7 KB · Views: 27
  • RQ5.png
    RQ5.png
    85.2 KB · Views: 19
  • RQ6.png
    RQ6.png
    53.1 KB · Views: 20
  • RQ2.png
    RQ2.png
    163.2 KB · Views: 24

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I take it that the only direction the summed cell could move after deleting rows is up? I'm not understanding why you need to worry about where it ends up in that case. In other words, if there was only one item (e.g. the 156"x3" one) why wouldn't the summing section just come right after that? Or it does but you don't want it to?

BTW, probably not an issue but you should know that X and FRow variables are Variants because they are not explicitly typed. Maybe you know that and it was intentional. If not, should be
Dim X As Long, FRow As Long, LRow As Long, etc.
 
Upvote 0
Can you not use a simple offset in that cell, like this:
Excel Formula:
=SUM($C$4:(OFFSET(C10,-1,0)))

Change $C$4 to be an absolute reference to your first cell in the sum column, and change C10 to be the relative reference for the same cell you're entering the total in. Example:
Offset Sum.jpg
 
Upvote 0
I take it that the only direction the summed cell could move after deleting rows is up? I'm not understanding why you need to worry about where it ends up in that case. In other words, if there was only one item (e.g. the 156"x3" one) why wouldn't the summing section just come right after that? Or it does but you don't want it to?

BTW, probably not an issue but you should know that X and FRow variables are Variants because they are not explicitly typed. Maybe you know that and it was intentional. If not, should be
Dim X As Long, FRow As Long, LRow As Long, etc.
The summed cell would only move up, that is correct. Currently I have it so that the sheet is does sum it prior to copy and pasting it. I want to make the new sheet have the sum formula after everything is copy pasted so that when I add the other items (Blueprints, installation, freight, etc.) those will auto sum for the user as well. I have the sheet protecting itself after as well so it can't be a manual input post new workbook creation.
 
Upvote 0
Can you not use a simple offset in that cell, like this:
Excel Formula:
=SUM($C$4:(OFFSET(C10,-1,0)))

Change $C$4 to be an absolute reference to your first cell in the sum column, and change C10 to be the relative reference for the same cell you're entering the total in. Example:
View attachment 93684
That is a pretty good use of that function. I have never thought of that before. This could potentially work. The problem is that I need the sum cell to generate after the new workbook is made and all cells are copy and pasted as values. The cell that this would go into is the "Total Investment" cell so that after the user finalizes the sheet they can enter the ancillary items (freight, installation, etc.) and everything will total up.
 
Upvote 0
I'm not sure how that last sheet is generated, but a few ideas come to mind, depending on what suits you best:
  1. Name the Total cell so that you can place that formula into the cell with VBA. You should be able to replace the C10 above with something like total_cell.Address.
  2. Keep that Total cell formula the same, and just hide the rows that you're not using instead of deleting them. I think that might be as simple as replacing Rows(x).Delete with Rows(x).Hidden = True
With number 2, you just need to make sure there's no numbers left behind that are hidden but still adding to the sum.
 
Upvote 0
Solution
I'm not sure how that last sheet is generated, but a few ideas come to mind, depending on what suits you best:
  1. Name the Total cell so that you can place that formula into the cell with VBA. You should be able to replace the C10 above with something like total_cell.Address.
  2. Keep that Total cell formula the same, and just hide the rows that you're not using instead of deleting them. I think that might be as simple as replacing Rows(x).Delete with Rows(x).Hidden = True
With number 2, you just need to make sure there's no numbers left behind that are hidden but still adding to the sum.
That could very much work! I appreciate your help on this!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top