简体   繁体   English

Excel VBA - 如何在一个单元格中识别年份(日期),然后检查另一个单元格中的数据,然后计算

[英]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. Excel VBA 在这里相当新,我现在正在参加一些付费课程以变得更好,但我遇到了障碍,谷歌搜索并没有让我得到我正在寻找的结果。

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.我想每年都这样做,所以 2017 年、2018 年、2019 年、2020 年等等等等。

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.例如,搜索包含我们处理 2017 年数据的日期的列 J,然后查看同一行中的已审计列(也包含日期),如果其中有任何内容并计数 1。然后一次又一次地执行直到整个范围都经过检查。 Each time we find a date from 2017 we add one and we do this for each year.每次我们找到 2017 年的日期时,我们都会添加一个,并且每年都会这样做。 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.要么是“next without for”,要么是我现在不记得了。 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?我找到了 SUMPRODUCT,我想我可以用它来计算 2017 年出现在我的列中的次数,但是我如何让它把它加起来并把它放到仪表板上? eg SUMPRODUCT(1*(YEAR(J1:J500)=2017))例如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.我被“这个 excel VBA 的东西很有趣”的错误所困扰。

You are nesting the FOR and the IF wrong.您嵌套了 FOR 和 IF 错误。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA按钮可使用日期条件将数据从一个单元格粘贴到另一个单元格 - Excel vba button to paste data from one cell into another cell using date criteria 检查一个单元格是否包含与另一单元格VBA完全相同的数据 - Check if one cell contains the EXACT same data as another cell VBA Excel 使用 VBA 将单元格数据从一张纸复制到另一张纸 - Excel Copy Cell Data from one sheet to another using VBA Excel / VBA-根据位置将多行合并为一-如何检查单元格值,然后将值保存在另一个单元格中? - Excel / VBA - Multiple Rows to One Based on Positioning - How do i check cell value then save the value in another cell? 如何计算 excel vba 中特定单元格数据的行数 - how to count the number of rows with data from a specific cell in excel vba Excel VBA检查其他单元格中是否包含单元格单词 - Excel VBA check if cell words contained in another cell 让 Excel 根据日期将数据从一个单元格复制到另一个单元格 - Getting Excel to Copy Data From One Cell to Another Depending on Date 如何在Excel VBA中将单元格从一行复制到另一行 - How to copy cell from one row to another in excel vba Excel VBA尝试将单元格值设置为来自另一个单元格的日期 - Excel VBA Attempting to set cell value as date from another cell Excel VBA如何将单元格内容从一本书复制到另一本书 - Excel VBA how to copy cell content from one book to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM