所以我有一个双循环,由于某种原因,它总是跳到我的外循环。循环来自:
For j = lastColumn To 6 Step (-1)
至:
Next i
每一次。但是,在我的数据集中,应该在if语句中捕获各种数据,并对数据进行计数。
有什么想法吗?也许我格式化宏的格式错误。
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 = 3
j = lastColumn
For i = 3 To lastrow
For j = lastColumn To 6 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
'Runs the DeleteSAC Macro
Call DeleteSAC
End Sub
答案 0 :(得分:0)
我不太了解您的操作,请尝试执行此操作。注意Exit For
。这应该会产生您想要的结果,而不会混淆For-Next计数器变量。
For i = 3 To lastrow
For j = lastColumn To 6 Step (-1)
If Sheet1.Cells(i, j) < Sheet2.Cells(1, 1) And Sheet1.Cells(i, j - 1) = "Reçu" Then
count = count + 1
Exit For
End If
Next j
Next i
答案 1 :(得分:0)
对行使用 Long ,对列使用 Integer 。
当您写 With Sheet1
时,应在任何应写Sheet1
的地方,例如Sheet1.Range(whatever)...
只能写成.Range(whatever)
,直到用 End With 结尾。
退出仅退出其所在的For循环。因此,它与转到行完全一样,但是您使用的行更多
当您使用Sheet1或Sheet2等时,实际上是在使用代码名称,因此您可以在选项卡中更改名称,代码仍将运行。
当您逐行删除时通常使用向后计数,因此无需这样做,因为您只是在计数。
Option Explicit
Sub CheckDates()
Dim dataCount As Long
Dim i As Long
Dim j As Integer
Dim lastrow As Long
With Sheet1
lastrow = .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 = .Cells(1, .Columns.count).End(xlToLeft).Column
For i = 3 To lastrow
For j = 6 To lastColumn
If .Cells(i, j) < Sheet2.Cells(1, 1) And .Cells(i, j - 1) = "Reçu" Then
dataCount = dataCount + 1
Exit For
End If
Next
Next
End With
With Sheet2
.Cells(1, 7) = dataCount
.Select
End With
'Runs the DeleteSAC Macro
Call DeleteSAC
End Sub