Excel vba检查h列中是否存在值,m列中的日期月份是否为本月?

Excel vba检查h列中是否存在值,m列中的日期月份是否为本月?,excel,vba,Excel,Vba,我正在尝试使用vba对第2页“日志”中的行进行计数,其中h列与第1页“主页”中输入到我的单元格中的值相匹配。我使用以下代码执行此操作: Dim iVal As Integer iVal = Application.WorksheetFunction.CountIf(Sheets("Logs").Columns("H"), Range("N10").Value) iVal2 = Application.WorksheetFunction.CountIf(Sheets("Logs")

我正在尝试使用vba对第2页“日志”中的行进行计数,其中h列与第1页“主页”中输入到我的单元格中的值相匹配。我使用以下代码执行此操作:

Dim iVal As Integer
    iVal = Application.WorksheetFunction.CountIf(Sheets("Logs").Columns("H"), Range("N10").Value)
    iVal2 = Application.WorksheetFunction.CountIf(Sheets("Logs").Columns("J"), Range("N20").Value)

If IsError(Application.Match(Range("N10").Value, Sheets("Logs").Columns("H"), 0)) Then
    MsgBox "No Match"
    Else
    MsgBox "Hi " & Range("N10").Value & "," & vbNewLine & vbNewLine & "Your department has requested " & iVal2 & " suppliers this month. You have " & 5 - iVal & " requests remaining for this month." & vbNewLine & vbNewLine & "Each department is allowed up to 5 new supplier requests per month.", vbOKOnly + vbInformation, "Important Notice!"
    Exit Sub
    End If
现在,我想添加到if语句中,并说,如果第2页第M列中同一行的日期包含当前月份和年份,则仅使用第1页我的单元格中输入的值对第h列的匹配值进行计数

例如,假设今天的月份是“2014年10月”,h列包含“苹果”一词,M列中的同一行是“2014年10月21日”

若我在表格1的单元格中输入“苹果”,那个么这个月苹果这个词将出现1次

但是,如果在我的h列中我有“苹果”,在m列中是“2014年9月21日”,而今天的月份是2014年10月,那么这将不会被计算在内,因为该月份不是当前月份

有人知道我能做这件事的方法吗?我试图用另一个if语句来包围我的代码,就像这样,但我是VBA新手,我不认为这是一个好办法

> If Month(Date) = Month(Sheets("Logs").Columns("M")) Then
>     MsgBox "Date now"
>     Else
>     MsgBox "Date not now"
>     End If

您不需要严格要求VBA来实现这一点。假设Sheet1和Sheet2上的数据网格均为99行,则使用本机Excel函数的以下公式将返回Sheet1的计数!H1:99匹配N10和表2!M1:99匹配N20的年份和月份=Sum1$H$1:$H$99=N10*TEXTSheet2$M$1:$M$99,yyyymm=TEXTN20,yyyymm

附录:如果完全限定单元格引用,只需使用方括号计算公式,如下所示:

Sub test()
    With ActiveSheet
        .Range("A1") = [=SUMPRODUCT((Sheet1!$H$1:$H$99=Sheet3!N10)*(TEXT(Sheet2!$M$1:$M$99,"yyyymm")=TEXT(Sheet3!N20,"yyyymm")))]
    End With
End Sub

请注意,我已经为N10和N20添加了工作表参考。

请尝试这一可能的解决方案,它需要一些“精细处理”,例如错误检查等,请提交给您

对于我的测试,工作表“主页”和日志如下所示,日期单元格的格式为日期。如果您需要在不同的工作表上使用不同的日期格式,那么这是您需要改进的。将按钮分配给下面名为“CountMatches”的代码。这种方法使用自动筛选。大多数参数都可以轻松设置,以便您根据自己的情况进行调整

床单

代码


谢谢,感谢你的努力,但我真的需要这个在vba?
Sub CountMatches()
Dim wsLogs As Worksheet, wsHome As Worksheet
Dim lstrow As Long, lendrow As Long, lstcol As Long, lendcol As Long
Dim crit1row As Long, crit1col As Long, crit2row As Long, crit2col As Long
Dim rsltRow As Long, rsltCol As Long, rslt As Long
Dim fndrng As Range

Set wsLogs = Sheets("Logs")
Set wsHome = Sheets("Home")
lstrow = 1
lstcol = 8: lendcol = 13    'col H-M
crit1row = 3: crit2row = 3
crit1col = 3: crit2col = 5
rsltRow = 3: rsltCol = 7

'clear autofilter
wsLogs.AutoFilterMode = False

    With wsLogs
        lendrow = .Cells(Rows.Count, lstcol).End(xlUp).Row
            With .Range(.Cells(lstrow, lstcol), .Cells(lendrow, lendcol))
                crit1 = wsHome.Cells(crit1row, crit1col).Value
                crit2 = wsHome.Cells(crit2row, crit2col).Value
                crit2 = DateSerial(Year(crit2), Month(crit2), Day(crit2))
                'set autofilter
                .AutoFilter
                .AutoFilter Field:=1, Criteria1:=crit1
                .AutoFilter Field:=6, Criteria1:="=" & crit2
                    With wsLogs.AutoFilter.Range
                        Set fndName = .SpecialCells(xlCellTypeVisible)
                    End With
            End With

            With .AutoFilter.Range
                 On Error Resume Next
                   Set fndrng = .Offset(1, 0).Resize(.Rows.Count - 1, 6) _
                       .SpecialCells(xlCellTypeVisible)
                       rslt = .Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
                       'MsgBox .Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
                 On Error GoTo 0
            End With

            With wsHome
                 .Range(.Cells(rsltRow, rsltCol), .Cells(rsltRow, rsltCol)) = rslt
            End With

    wsLogs.AutoFilterMode = False

    End With

End Sub