我正在尝试使用vba来计算工作表2上的行('日志'),其中列h与在工作表1中输入到我的单元格中的值相匹配(' home')。我使用以下代码执行此操作:
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语句中,并说只计算列h的匹配值和在表1中的单元格中输入的值,如果表2中M列的同一行中的日期包含当前月份和年。
所以比如说,今天的月份是' 2014年10月'和列h包含单词' apples'并且在M列的同一行中是' 21/10 / 2014'。
如果我输入' apples'在我的单元格1中,它将计算本月出现的单词apple。
但如果在我的专栏中,我有“苹果”等。在第m栏是' 21/09 / 2014'而今天的月份是2014年10月,那么这个月不会计算当月。
有谁知道我可以做到这一点的方式?我试图用另一个if语句包围我的代码,但我是VBA的新手,我不认为这是要走的路。
> If Month(Date) = Month(Sheets("Logs").Columns("M")) Then
> MsgBox "Date now"
> Else
> MsgBox "Date not now"
> End If
答案 0 :(得分:0)
您并不严格要求VBA来完成此任务。假设Sheet1和Sheet2上的数据网格为99行,则使用本机Excel函数的以下公式将返回Sheet1的计数!H1:99匹配N10和Sheet2!M1:99匹配年份和月份N20。 =SUMPRODUCT((Sheet1!$H$1:$H$99=N10)*(TEXT(Sheet2!$M$1:$M$99,"yyyymm")=TEXT(N20,"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的工作表参考。
答案 1 :(得分:0)
请尝试将此作为一种可能的解决方案,需要一些'finessing',例如错误检查等 - 过来给你!
对于我的测试,Sheets'Home'和“Logs”如下所示,Date单元格被格式化为Date。如果您在不同的纸张上需要不同的日期格式,那么这将是您的技巧。将按钮分配给名为“CountMatches”的代码(如下所示)。此方法使用AutoFilter。您可以轻松设置大多数参数,以便根据您的情况进行调整。
表格
守则
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