Routine To Add Autosums in 3 different cells based on the value in another cell

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to add numerous autosum's to the worksheet I am working on using VBA and to say I am struggling is an understatement. Below is a link to a picture that show what I am trying to do.

https://photos.app.goo.gl/kt4kp8uULuM4hWvFA

I need a routine to check the whole worksheet as it varies in length and only when "Total Lines" is the value in column P does it need to enter the autosum's in columns M, N & O in the adjacents cells on the same row.

The issue I have is that directly above where the autosums need to be entered there is a blank row, also above the data to be autosummed there is another blank row to give the autosum the required cut off.


If anyone can help with this is would be greatly appreciated.

Thanks in advance
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Bit of a guess without knowing what else is on the sheet, but give this a try in a copy of your workbook.
I have assumed that the "Totals" texts in column L are not the results of formulas

Code:
Sub Add_Totals()
  Dim cell As Range
  
  For Each cell In Range("L3", Range("L" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlTextValues)
    cell.Offset(, 1).Resize(, 3).FormulaR1C1 = "=SUM(R" & cell.Offset(-2).CurrentRegion.Row & "C:R[-2]C)"
  Next cell
End Sub
 
Upvote 0
Many thanks for you help, I have tried this however it appears to be totalling all sections together whereas it should only total between the blanks, apologies if I didn't explain properly


Bit of a guess without knowing what else is on the sheet, but give this a try in a copy of your workbook.
I have assumed that the "Totals" texts in column L are not the results of formulas

Code:
Sub Add_Totals()
  Dim cell As Range
  
  For Each cell In Range("L3", Range("L" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlTextValues)
    cell.Offset(, 1).Resize(, 3).FormulaR1C1 = "=SUM(R" & cell.Offset(-2).CurrentRegion.Row & "C:R[-2]C)"
  Next cell
End Sub
 
Upvote 0
Many thanks for you help, I have tried this however it appears to be totalling all sections together whereas it should only total between the blanks, apologies if I didn't explain properly
Although the formulas are apparently wrong, is the code putting formulas in the correct cells?
 
Upvote 0
Try this one then
Code:
Sub Add_Totals_v2()
  Dim cell As Range
  Dim fr As Long
  
  For Each cell In Range("L3", Range("L" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlTextValues)
    fr = 4
    On Error Resume Next
    fr = Range("O1", Range("O" & cell.Row)).Find(What:="OTIF", SearchDirection:=xlPrevious).Row + 2
    On Error GoTo 0
    cell.Offset(, 1).Resize(, 3).FormulaR1C1 = "=SUM(R" & fr & "C:R[-2]C)"
  Next cell
End Sub
 
Last edited:
Upvote 0
Excellent that works an absolute treat, thank you for this it's greatly appreciated.

Try this one then
Code:
Sub Add_Totals_v2()
  Dim cell As Range
  Dim fr As Long
  
  For Each cell In Range("L3", Range("L" & Rows.Count).End(xlUp)).SpecialCells(xlConstants, xlTextValues)
    fr = 4
    On Error Resume Next
    fr = Range("O1", Range("O" & cell.Row)).Find(What:="OTIF", SearchDirection:=xlPrevious).Row + 2
    On Error GoTo 0
    cell.Offset(, 1).Resize(, 3).FormulaR1C1 = "=SUM(R" & fr & "C:R[-2]C)"
  Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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