我正在尝试SUMPRODUCT
在 VBA 中使用以下公式:
=SUMPRODUCT(--('Sheet 1'!$C$2:$C$10="OK"),(MONTH(Sheet1!$A$2:$A$10)=MONTH($B3))*(YEAR(Sheet1!$A$2:$A$10)=YEAR($B3))*(MONTH(Sheet1!$B$2:$B$10)=MONTH(C$2))*(YEAR(Sheet1!$B$2:$B$10)=YEAR(C$2)))
我的工作簿与航班预订有关,它由两张表组成,表 1 和表 2。具体而言:
表 1 包括示例数据:
| Booking date | Check-in date | Status |
|--------------|---------------|--------|
| 25-Sep-18 | 18-May-19 | OK |
| 26-Sep-18 | 05-Jul-19 | OK |
| 26-Sep-18 | 18-Jun-19 | OK |
| 01-Oct-18 | 19-Jun-19 | OK |
| 01-Oct-18 | 18-May-19 | OK |
| 02-Oct-18 | 19-Apr-19 | OK |
| 03-Oct-18 | 18-May-19 | CL |
| 06-Oct-18 | 14-May-19 | CL |
| 07-Oct-18 | 17-Jun-19 | OK |
SUMPRODUCT
而表 2 包括与公式相关的分析输出:
| Booking/Check-in | Apr-19 | May-19 | Jun-19 | Jul-19 |
|------------------|--------|--------|--------|--------|
| Sep-18 | | 1 | 1 | 1 |
| Oct-18 | 1 | 1 | 2 | |
我按照@Rosetta 和@Foxfire And Burns And Burns 的建议修复了错误'13' - 类型不匹配。我还按照@JvdV 的建议添加了 Else。现在代码如下所示:
Dim bk As Worksheet
Set bk = Worksheets("Sheet 1")
For Z = 2 To 3
For j = 2 To 5
For i = 2 To 10
If bk.Cells(i, 3).Value = "OK" Then
If Month(bk.Cells(i, 1)) = Month(Cells(Z, 1)) Then
If Year(bk.Cells(i, 1)) = Year(Cells(Z, 1)) Then
If Month(bk.Cells(i, 2)) = Month(Cells(1, j)) Then
If Year(bk.Cells(i, 2)) = Year(Cells(1, j)) Then
Range(Cells(j, Z)).Value = Range(Cells(j, Z)).Value + 1
Else Next i
End If
End If
End If
End If
End If
Next j
Next Z
End Sub
现在我得到Next without For
编译错误。有任何想法吗?