Sum the values between two specified column headings - number of columns between will change

Moley84

New Member
Joined
Jul 11, 2024
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Good morning all,

My column headings are in row 7. The below code works fine. However, the ActiveCell.Formula = "=G8" needs changing (the active cell is the Subtotal column). I need it to sum the values between two columns called "Calculated payment based on time and costs" and "Subtotal" and then multiple that value by what's in column G. For example, as per screenshot, =(SUM(H18:K18))*G18. If these columns were static, I would be fine but the number of columns between these columns will change, meaning that the K18 part of the formula above would need to change. The "Calculated payment based on time and costs" will always be in column G but the "Subtotal" column will change. I'm guessing I need to use Find(What:= which is already in this code for another reason but I don't know how to incorporate that into code with a formula being calculated as well.

Any help most gratefully received :). Thank you.

VBA Code:
'Calculate the far right columns
'Medical
 ' Subtotal
    Dim RRMedicalFinal
    RRMedicalFinal = Sheets("PatientLevelCostsMedical").Cells(Rows.Count, "B").End(xlUp).Row

    Sheets("PatientLevelCostsMedical").Activate
    Sheets("PatientLevelCostsMedical").Rows(7).Find(What:="Subtotal", LookAt:=xlWhole).Activate
    ActiveCell.Offset(1).Activate
    ActiveCell.Formula = "=G8"
    ActiveCell.NumberFormat = "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* ""-""_-;_-@_-"
    If RRMedicalFinal = 8 Then GoTo AfterAutoFillNurse1
    ActiveCell.AutoFill Destination:=Sheets("PatientLevelCostsMedical").Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(RRMedicalFinal, ActiveCell.Column))
    Sheets("PatientLevelCostsMedical").Range(Cells(ActiveCell.Row, ActiveCell.Column), Cells(RRMedicalFinal, ActiveCell.Column)).NumberFormat = "_-$* #,##0.00_-;-$* #,##0.00_-;_-$* ""-""_-;_-@_-"
AfterAutoFillMedical1:
1729670347558.png
 
Hi, sorry, me again.

I just tried this on another sheet which has a lot more columns. Therefore, Range("L18") needs to change to using something like INDEX or FIND?

I tried a combination of both yours and HighandWilder's, ie ActiveCell.Formula = "=SUM(" & Range("H8").Address(False, False) & ":" & "INDEX(8:8,1,MATCH(""Subtotal"",$7$7,0)-1))*" & Range("G8").Address(False, False) but am getting the same error again. Where have I gone wrong, please?
Why are you trying to do this using VBA when a formula option is easier?

Did the mini-sheet that I posted work?

Which cell is the Active Cell when you run this code?
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Why are you trying to do this using VBA when a formula option is easier?

Did the mini-sheet that I posted work?

Which cell is the Active Cell when you run this code?
Hi, The vba code imports data and then manipulates it to different sheets. It needs to then recalculate the Subtotal field as the data has been split across sheets. That's why it's in vba. This is for a team to use, so I can't expect them to enter formula into sheets.
 
Upvote 0
Why are you trying to do this using VBA when a formula option is easier?

Did the mini-sheet that I posted work?

Which cell is the Active Cell when you run this code?
Hi, sorry, didn't answer your questions. The active cell is the cell beneath the word Subtotal.

I could see that your spreadsheet worked. I just need to add the formula within vba script. :)
 
Upvote 0
I tried a combination of both yours and HighandWilder's, ie ActiveCell.Formula = "=SUM(" & Range("H8").Address(False, False) & ":" & "INDEX(8:8,1,MATCH(""Subtotal"",$7$7,0)-1))*" & Range("G8").Address(False, False) but am getting the same error again. Where have I gone wrong, please?
You are missing the colon in $7:$7.
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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