因此,我有一段循环的代码,如果存在带有相应日期的特定状态,则将其计数为1。通常格式如下:状态,日期,状态,日期,状态,日期等...
但是,我的数据集已更改为状态,日期,人,状态,日期,人,状态,日期,人等...
我以前使用步骤(-2),但是现在当我更改为步骤(-1)甚至步骤(-3)时,它给了我一个应用程序定义或对象定义的错误。
有人知道为什么吗?
Sub CheckDates()
Dim count As Integer
Dim i As Integer
Dim j As Integer
Sheets(1).Select
lastrow = ActiveSheet.Cells(Rows.count, "B").End(xlUp).Row
'have to keep data in a table for this to actually work as it ctrls+left to the table, which will end where the very last text of any row is
lastColumn = ActiveSheet.Cells(1, Columns.count).End(xlToLeft).Column
count = 0
i = 2
j = lastColumn
For i = 2 To lastrow
For j = lastColumn To 1 Step (-1)
If Sheet1.Cells(i, j) < Sheet2.Cells(1, 1) And Sheet1.Cells(i, j - 1) = "Reçu" Then
count = count + 1
GoTo NextIteration
End If
Next j
NextIteration:
Next i
Sheet2.Cells(1, 7) = count
Sheets(2).Select
Call DeleteSAC
'Runs the DeleteSAC Macro
End Sub
答案 0 :(得分:1)
看这行
If Sheet1.Cells(i, j) < Sheet2.Cells(1, 1) And Sheet1.Cells(i, j - 1) = "Reçu" Then
如果j = 1,则j-1将为0,并且您会收到错误消息(即不存在第0列)
使用Step -2
不会导致错误,因为(如果我不得不猜测)列数是偶数,即j的最后一个值为2。在下一次迭代时j将为0并循环主体根本不会执行,因此没有错误。