简体   繁体   中英

Excel VBA - how to identify the year (date) in one cell then check another cell for the data there and then count

Help. Fairly new at Excel VBA here and I'm taking some paid courses right now to get better but I've run into a road block and googling isn't getting me the results I'm looking for.

I have an ever growing tracking spreadsheet which shows data about what we've processed (we call it "building it") in a column and then data showing we've checked (we call it as having been audited) that data we've processed in other column. I want to find the year is was processed and find if it was audited regardless of the year of the audit and if so add it up. I want to do this for every year, so 2017, 2018, 2019, 2020 etc. etc.

Then finally place the findings for each year in a dashboard cell.

Eg search column J which contains a date we processed the data from 2017, then look at the audited column in the same row (which also contains a date) if there is anything in there and to count 1. Then do it again and again until the whole range has been checked. Each time we find a date from 2017 we add one and we do this for each year. At the end of the search the results for each year needs to be entered in the dashboard sheet.

Is there a way to write code for it to check every year instead of having to call out each year specifically?

Here's what I've gotten so far but I keep running into an error. Either "next without for" and others which I can't remember now. I've googled and added all sorts of stuff and moving it etc. etc. I'm sure I'm doing something really obviously wrong here but I'm just not seeing it. Please help a newbie out here.


Sub CountAudits()

Dim CellBuild As Range
Dim CellAudit As Range
Dim CellDateCount2017 As Long

For Each CellBuild In Sheet1.Range("J:J500") 'column J contains a date MMYYDD what we call "build" data in other words we processed it.

If CellBuild.Value = "2017" Then 'this checks if the date it was processed was in 2017

For Each CellAudit In Sheet1.Range("Q:Q500") 'this is the column containing the date it was audited. It will be blank if it was not audited.

If CellAudit.Value <> "" Then 'this checks if it's been audited. I don't care about the date just as long a there is something in the cell

CellDateCount2017 = CellDateCount2017 + 1 'if the processed date was 2017 and it was audited it adds a count of 1 to here.

Exit For

Sheet2.Range("V18").Value = CellDateCount2017 'Sheet2 is the sheet code name to the dashboard

Next ' I keep getting an "Next without for" error for this here. I've added all sorts of ends, end ifs, etc. but I can't figure it out.
End If

End Sub

I found SUMPRODUCT which I think I can use to count the number of times 2017 shows up in my column but how to I get it to add it up and get it over to the dashboard? eg SUMPRODUCT(1*(YEAR(J1:J500)=2017))

If only it wasn't so much fun to figure this stuff out I could just walk away from it. I've been bitten by the "this excel VBA stuff is fun" bug.

You are nesting the FOR and the IF wrong. Try the code below. I didn't check for functionality, I only corrected the loops:

Sub CountAudits()

Dim CellBuild As Range
Dim CellAudit As Range
Dim CellDateCount2017 As Long

    For Each CellBuild In Sheet1.Range("J:J500") 'column J contains a date MMYYDD what we call "build" data in other words we processed it.

        If CellBuild.Value = "2017" Then 'this checks if the date it was processed was in 2017

            For Each CellAudit In Sheet1.Range("Q:Q500") 'this is the column containing the date it was audited. It will be blank if it was not audited.

                If CellAudit.Value <> "" Then 'this checks if it's been audited. I don't care about the date just as long a there is something in the cell

                CellDateCount2017 = CellDateCount2017 + 1 'if the processed date was 2017 and it was audited it adds a count of 1 to here.

                End If

            Next CellAudit

            Sheet2.Range("V18").Value = CellDateCount2017 'Sheet2 is the sheet code name to the dashboard

        End If

    Next CellBuild

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM