vba to sumif the value by year

abhi_jain80

New Member
Joined
May 31, 2021
Messages
27
Office Version
  1. 2016
Platform
  1. Windows
Hi experts,

I need to sumif the values of column "AO" if the year in column "AN" is 2021 and need the result in different worksheet range say A2. can somebody help me please?
Mini sheet attached for your reference. As per this sample data, the result should be 296

Apologies if this has been solved earlier and thanks in advance...

Spares Analytics ToolKit_v3.xlsb
ANAO
1Date$ Value
221/06/202134
321/07/202132
401/06/20213
517/06/20212
612/06/20212
726/08/20221
801/06/20211
901/06/20201
1001/06/20203
1101/06/20203
1201/06/20212
1302/06/20213
1408/02/2020442
1508/03/202222
1605/06/202112
1711/09/202121
1803/06/20210
1918/05/2021184
2027/01/2022-87
Analytics Summary
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Not sure why you need VBA when a simple formula will do:
Excel Formula:
=SUMPRODUCT(--(YEAR(AN2:AN20)=2021),--(AO2:AO20))
 
Upvote 0
Not sure why you need VBA when a simple formula will do:
Excel Formula:
=SUMPRODUCT(--(YEAR(AN2:AN20)=2021),--(AO2:AO20))
This is a part of big vba script, hence i need the vba code only. I am trying to use the formula you have suggested but not been able to. can you please have a look?

VBA Code:
    Dim lastRow As Long
    With Worksheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "AM").End(xlUp).Row
    End With
    
    With Worksheets("Sheet2").Range("A1")
        .Formula = "=SUMPRODUCT(--(YEAR('Sheet1'!AN2:AN" & lastRow & "=2021")),--('Sheet1'!AO2:AO" & lastRow & "))"
        .Value = .Value
    End With
   End Sub
 
Upvote 0
Temporarily remove the:
VBA Code:
        .Value = .Value
line.

Then it will leave the formula in there, and you can verify if it looks right.
 
Upvote 0
Temporarily remove the:
VBA Code:
        .Value = .Value
line.

Then it will leave the formula in there, and you can verify if it looks right.
The formula doesn't looks right. I am not able to correct it. Can you please suggest the right formula? Thanks
 
Upvote 0
Please post exactly what the formula looks like.
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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