VBA - Intersect at Column values 'Total' and 'Oranges' . Sum offset 2 columns to left.

MikeL

Active Member
Joined
Mar 17, 2002
Messages
492
Office Version
  1. 365
Platform
  1. Windows
Hello,
I want to add an offset formula to sum two columns to the left of the selected cell.

The selected cell will be the intersection of the column field 'Total' and the row value 'Oranges'. These values create an intersection 3x in the attached worksheet sample.

Thanks in advance
CATEGORY2018Q1-Q22018Q3-Q4TOTAL2019Q1-Q22019Q3-Q4TOTAL2020Q1-Q22020Q3-Q4TOTAL
BERRIES151203180
ORANGES10122231013111021
APPLES348263
ORANGES510154101492029
PEARS348263
ORANGES102030210127512
BERRIES348263
 

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.
What columns and rows can we see? When using XL2BB if you use Mini Sheet instead of Table Only, we would be able to see that information. :)

You only want to insert a formula for oranges and not the other values?

BTW, I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Hi, Yes only for rows with 'Oranges'. XL 2013, Windows updated
VBA_SAMPLE.xlsx
ABCDEFGHIJ
1CATEGORY2018Q1-Q22018Q3-Q4TOTAL2019Q1-Q22019Q3-Q4TOTAL2020Q1-Q22020Q3-Q4TOTAL
2BERRIES151203180
3ORANGES10122231013111021
4APPLES348263
5ORANGES510154101492029
6PEARS348263
7ORANGES102030210127512
8BERRIES348263
AFTER
Cell Formulas
RangeFormula
D3,J7,G7,D7,J5,G5,D5,J3,G3D3=SUM(B3:C3)
 
Upvote 0
Thanks for updating your profile and for the new mini-sheet. (y)

See if this does what you want. Test with a copy of your workbook.

VBA Code:
Sub Total_Oranges()
  Application.ScreenUpdating = False
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .AutoFilter Field:=1, Criteria1:="ORANGES"
    If .SpecialCells(xlVisible).Count > 1 Then Intersect(.Offset(1).Resize(.Rows.Count - 1).EntireRow, Range("D:D,G:G,J:J")).FormulaR1C1 = "=RC[-2]+RC[-1]"
    .Parent.AutoFilterMode = False
  End With
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Range("D:D,G:G,J:J"))
Thanks Peter. The Range where 'Total' is variable. It could be different columns and can range from 1 column to 6 columns in the Workbook. Any update for this piece of code?
 
Upvote 0
Assuming that it is still adding the 2 columns to the left as you originally described, and 'CATEGORY' is still in cell A1, try

VBA Code:
Sub Total_Oranges_v2()
  Dim rCol As Range
  Dim DataRows As Long
  
  Application.ScreenUpdating = False
  With Range("A1").CurrentRegion
    DataRows = .Rows.Count - 1
    .AutoFilter Field:=1, Criteria1:="ORANGES"
    If .Columns(1).SpecialCells(xlVisible).Count > 1 Then
      For Each rCol In .Columns
        If rCol.Cells(1).Value = "TOTAL" Then rCol.Offset(1).Resize(DataRows).SpecialCells(xlVisible).FormulaR1C1 = "=RC[-2]+RC[-1]"
      Next rCol
    End If
    .Parent.AutoFilterMode = False
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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