如何在 vba 中的循环末尾实现公式?
How to implement a formula at the end of your loop in vba?
我试图从本质上有一个为我计算某些标准的循环。在我的代码末尾,我想将未计算在内的内容本质上称为 "DRSNR"。我知道我可以在我的文件中使用一个简单的公式来让它工作,但我宁愿在我的 VBA 代码中使用这个公式,这样它就变成了伪证,并且没有人会错误地删除它,因为我不会是唯一的用户。
现在由于某些原因我的代码在这个地方不起作用:
drsnr = lastrow - sac - count
出于某种原因,无论如何我总是得到 0
的计数。有任何想法吗?我首先认为我的 lastrow
不是整数,所以我添加了 Dim lastrow as integer
,但是这似乎也不起作用。
Sub DeleteSAC()
Dim count As Integer
Dim sac As Integer
Dim drsnr As Integer
Dim i As Integer
Dim j As Integer
Dim lastrow 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 = Sheet2.Cells(1, 7).Value
sac = 0
i = 2
j = lastColumn
For i = 2 To lastrow
For j = lastColumn To 1 Step -1
If Sheet1.Cells(i, j) = "SAC" Or Sheet1.Cells(i, j) = "Incorrect address" Then
count = count - 1
sac = sac + 1
GoTo NextIteration
End If
Next j
NextIteration:
Next i
Sheet2.Cells(1, 7) = count
Sheet2.Cells(1, 10) = sac
Sheet2.Cells(1, 13) = drsnr
drsnr = lastrow - sac - count
Sheets(2).Select
End Sub
您可能需要移动此行:
drsnr = lastrow - sac - count
之前:
Sheet2.Cells(1, 13) = drsnr
drsnr
将是一个初始化的 Integer
,值为 0
,直到您为其分配其他值。
我试图从本质上有一个为我计算某些标准的循环。在我的代码末尾,我想将未计算在内的内容本质上称为 "DRSNR"。我知道我可以在我的文件中使用一个简单的公式来让它工作,但我宁愿在我的 VBA 代码中使用这个公式,这样它就变成了伪证,并且没有人会错误地删除它,因为我不会是唯一的用户。
现在由于某些原因我的代码在这个地方不起作用:
drsnr = lastrow - sac - count
出于某种原因,无论如何我总是得到 0
的计数。有任何想法吗?我首先认为我的 lastrow
不是整数,所以我添加了 Dim lastrow as integer
,但是这似乎也不起作用。
Sub DeleteSAC()
Dim count As Integer
Dim sac As Integer
Dim drsnr As Integer
Dim i As Integer
Dim j As Integer
Dim lastrow 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 = Sheet2.Cells(1, 7).Value
sac = 0
i = 2
j = lastColumn
For i = 2 To lastrow
For j = lastColumn To 1 Step -1
If Sheet1.Cells(i, j) = "SAC" Or Sheet1.Cells(i, j) = "Incorrect address" Then
count = count - 1
sac = sac + 1
GoTo NextIteration
End If
Next j
NextIteration:
Next i
Sheet2.Cells(1, 7) = count
Sheet2.Cells(1, 10) = sac
Sheet2.Cells(1, 13) = drsnr
drsnr = lastrow - sac - count
Sheets(2).Select
End Sub
您可能需要移动此行:
drsnr = lastrow - sac - count
之前:
Sheet2.Cells(1, 13) = drsnr
drsnr
将是一个初始化的 Integer
,值为 0
,直到您为其分配其他值。