excel - 如何在 vba 中的循环结束时实现公式?
问题描述
我试图基本上有一个循环来计算我的某些标准。在我的代码结束时,我想使不计算的内容本质上称为“DRSNR”。我知道我可以在我的文件中使用一个简单的公式来使其工作,但我宁愿在我的 VBA 代码中使用这个公式,这样它就可以成为虚拟证明,并且没有人会错误地删除它,因为我不会是唯一的用户。
现在由于某种原因,我的代码在这个地方不起作用:
drsnr = lastrow - sac - count
0
出于某种原因,无论如何我总是得到计数。有任何想法吗?我首先认为 mylastrow
没有被归为整数,所以我添加了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
使用 value进行初始化0
,直到您为其分配其他值。
推荐阅读
- kendo-ui - 剑道多选过滤器不显示
- bash - 使用 FOR 循环将列表中的项目与 ARRAY 中的项目进行比较
- python - Django在表单中静态添加字段
- python - 如何通过云功能将 GCP 的安全中心资产导出到云存储?
- javascript - 触摸事件未在移动设备上触发
- ansible - 如何在 ansible 中使用 {{ item }}?
- php - 根据 WooCommerce 中的用户角色和购物车总数更改结帐时的订单按钮文本
- apache-spark - Glue PySpark 作业因资源问题而失败
- vb.net - VB.Net 如何在等待外部 Web 服务数据加载时释放 UI
- python - Python plotly - 向图例添加水平线