Finding Number of Times a Specific Year Value is Used

kajohnson99

New Member
Joined
Dec 20, 2024
Messages
5
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
 
The answer to your question is yes. I can select all the sheet that I wanrt to sum across. I will try your formula.
Thanks,
Keith
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Here is a screen shot showing how I entere3d the code and the error message I get (#Name?)
Screenshot 2024-12-23 101659.png
Screenshot 2024-12-23 101719.png
 
Upvote 0
1) Insert a New Module and never name it as same as the function name.
2) copy the code and paste to 1)
Code:
Function CountByYear&(myYear&)
    Dim s$, ws As Worksheet
    For Each ws In Worksheets
        If UCase$(ws.Name) <> "TOC" Then
            s = ws.Range("c1", ws.Range("c" & Rows.Count).End(xlUp)).Address
            s = Replace("sum(if(isnumber(#),if(year(#)=" & myYear & ",1,0),0))", "#", s)
            CountByYear = CountByYear + ws.Evaluate(s)
        End If
    Next
End Function
3) Use in cell like
G5:
=CountByYear(F5)
 
Upvote 0

Forum statistics

Threads
1,224,858
Messages
6,181,431
Members
453,040
Latest member
Santero

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