Finding Number of Times a Specific Year Value is Used

kajohnson99

New Member
Joined
Dec 20, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm a novice at Excel VBA. I have a workbook with multiple sheets. In column C on each sheet, there may be a date value. I want to search each sheet and count the number of times the year value of the date is the same as the input year. The input year is in a specific cell on sheet TOC. I am calling the function from a cell in TOC.
I seem to go into an infinite loop with the system continually beeping at me. My function code is attached. some one tell what I'm doing wrong?

Public Function SumValuesByYear()

Dim ws As Worksheet
Dim SumYears As Long
Dim cell As Range
Dim TargetYear As Range
Dim nbCells As Integer
Dim sYear As Long


Set ws = ActiveSheet
sYear = Year(ActiveCell.Value)
'sYear = "2022"
SumYears = 0
' Loop through each worksheet
For Each ws In ActiveWorkbook.Worksheets
SumYears = 0
If ws.Name <> "TOC" Then
nbCells = 0
'sum values by year using the For Loop
For Each cell In ws.Columns(3).Cells
' For nbCells = 1 To 35
If IsDate(cell.Value) Then
If Year(cell.Value) = sYear Then
SumYears = SumYears + 1
End If
End If
' Next nbCells
Next cell
End If
Next ws
SumValuesByYear = SumYears
End Function
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try .
In any cell enter the function, If H3 in TOC sheet is the cell contains required year.
Excel Formula:
=SumValuesByYear('TOC'!H3)
VBA Code:
Public Function SumValuesByYear(Cel As Range)

Dim ws As Worksheet
Dim SumYears As Long
Dim sYear As Long
Dim Lr&, T&

sYear = Year(Cel.Value)
SumYears = 0
' Loop through each worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "TOC" Then
'sum values by year using the For Loop
Lr = 0
Lr = ws.Range("C" & Rows.Count).End(xlUp).Row
For T = 1 To Lr
If IsDate(ws.Cells(T, 3).Value) Then
If Year(ws.Cells(T, 3).Value) = sYear Then
SumYears = SumYears + 1
End If
End If
Next T
End If
Next ws
SumValuesByYear = SumYears
End Function
 
Upvote 0
Thanks for your response. I have entered your code as is. Please correct me if I'm wrong. I included it in the Modules section of my project. My concern is when I enter =SumValuesByYear(TOC!F5) in a cell, I get #NAME? error. And when I compile the function, I get no error but I can't step into the code to execute it step by step.
Can you help me to determine what I'm doing wrong?
Thanks,
Keith Johnson
 
Upvote 0
Is there a reason for VBA? Built-in formulas can accomplish the same.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

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