excel - VBA 嵌套 FOR 循环适用于第一次出现但忽略以下循环
问题描述
我正在努力使用嵌套在另一个循环中的 FOR 循环来水平检查表的每一行,然后再移动到下一行。
目标:我需要我的代码忽略所有值为 0 的单元格,并对非零单元格进行一些基本(不相关)操作。
问题:我的代码适用于该行的第一个非零单元格,但忽略了同一行和后续行中的所有其他单元格。
代码基本结构:
- FOR 循环 1:从一行到下一行;
- FOR 循环 2:(嵌套在循环 1 中)从行中的一个单元格到下一个单元格;
- IF THEN :(嵌套在循环 2 中)检查非零单元格并进行操作
我试图重新组织循环,以反转 IF Then 结构以首先忽略零单元格,但随后我找不到进入循环 2 下一次迭代的方法。
Dim RNB As Long, ROutput As Integer, VBAR As Integer, VBAC As Integer, i As Long, j As Long
'RNB = amounts of rows to check in input table;
'ROutput = the row number of the output table where to paste the desired values;
'VBAR = row number of the first cell to check in the Input table;
'VBAC = column number of the first cell to check in the Input table;
'i = the counting variable for the vertical FOR loop; and
'j = the counting variable for the horizontal FOR loop.
'Vertical parent FOR loop
For i = 1 To RNB
'Horizontal nested FOR loop: cells with a null value are ignored and the remaining ones are copied in the Output worksheet, in the first empty row available
For j = 1 To 11
If Cells(i + [2] - 1, [2] + 2 + j).Value <> 0 Then
'Copy/paste of the value of the identified cell and save the row number for the remaining information to add in next steps
[Input1].Select
Cells(i + [VBA_Row].Value - 1, [Segment].Value + 2 + j).Copy
'[Irrelevant code that actually works perfectly for the first iteration (I will display it if necessary)]
Else: End If
Next j
Next i
没有显示错误消息。
代码执行第一次迭代的工作,但通常如果第 1 行的前 2 个单元格不等于 0:代码执行单元格 1 的工作,忽略单元格 2 并转到下一行以忽略所有其他单元格。
解决方案
推荐阅读
- c++ - 如何正确绘制简单的非客户区(4 px 红色边框)?
- c# - .Net Core 2.0 项目上的 ImageProcessor.Web
- apache-kafka - 卡夫卡高取炼狱大小
- android - 获取 SD 卡的路径
- git - 尝试从 Visual Studio 2015 合并时出现 Git 错误
- java - Spring MVC:属性文件中的验证错误消息不显示
- r - 使用少数特定列上的条件对 R 中的数据框进行子集化
- clickatell - Clickatell 的新 API 是否像旧 API 一样使用 HTTP 响应代码?
- css - Flex:在 flex-start 对齐所有孩子但强制一个孩子结束?
- apache-nifi - 使用 nifi 处理器解压/解压 winRAR 文件