How do I divide 2 cells using VBA on a subtotal line

CStumpo

New Member
Joined
May 27, 2016
Messages
11
Hello there,
I have a simple subtotal macro where the last column, not included in the subtotal, needs to have the subtotals divided. This column has other results supplied by our financial system. As an example, column 11 is the result of column 10 / column 9. The subtotal macro subtotals columns 7, 8, 9 and 10 but not 11 - as it would be incorrect to subtotal 11. I need to take the subtotal in 9 and 10 and use them in the formula to get column 11. The subtotal occurs on 2 sheets within the workbook.

I have another workbook where I insert a blank line after the subtotal, I was wondering if this couldn't be used to do the formula but am not sure how to change is. Subtotal code and the line insert are below.
The last set of code is an attempt to try the formulas but not sure how to get them to work.

/code
Sheets("VP").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(7, 8, 9, 10)
Sheets("VP2").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(7, 8, 9, 10)

Dim c As Range
For Each c In Range("PROD")
If c.Value Like "*Total*" Then
c.Offset(1, 0).EntireRow.Insert
End If
Next c

'setting the cell references
A=.Cells("Totals", 9).Value
B=.Cells("Totals", 10).Value
.Cells("Totals",11)=B/A

[/code]
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I have worked on the coding for my question and gotten the first part to work. I am getting an Object required error on the "Cells(c.Row,11) = tempC / tempD" line
Any help would be appreciated. Thank you.

/code
Sheets("VP").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(7, 8, 9, 10)


Dim b As Range
For Each b In Range("VP")
If b.Value Like "*Total*" Then
tempA = Cells(b.Row, 10).Value
tempB = Cells(b.Row, 9).Value
Cells(b.Row, 11) = tempA / tempB
End If
Next b
'
Sheets("VP2").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(7, 8, 9, 10)


Dim c As Range
For Each c In Range("VP_2")
If c.Value Like "*Total*" Then
tempC = Cells(c.Row, 10).Value
tempD = Cells(c.Row, 9).Value
Cells(c.Row, 11) = tempC / tempD 'Line with Object required error'
End If
Next c
'
End Sub
[/code]
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,973
Members
449,059
Latest member
oculus

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