【发布时间】:2020-12-06 21:46:24
【问题描述】:
我有一个包含 10 列的 Excel 文件。在第 2、3、4 列中,我有一个数字或破折号。
如果这 3 个单元格的总和大于 1,我需要将整行替换为 n 行,其中只有一列的值为 1,但其他单元格保持不变。
Example
1 - - #-> leave it as is
- 2 - #-> replace that row with 2 rows : - 1 - ; - 1 -
2 - 1 #-> replace that row with 3 rows : 1 - - ; 1 - - ; - - 1;
我设法自下而上进行迭代,但我无法在内存中存储一行、对其进行操作并在下方插入。
Sub Test()
Dim rng As Range
Dim count20, count40, count45, total, i As Integer
Set rng = Range("A3", Range("A3").End(xlDown))
For i = rng.Cells.count To 1 Step -1
count20 = 0
count40 = 0
count45 = 0
total = 0
count20 = Cells(rng.Item(i).Row, 10).Value
If count20 > 1 Then
total = total + count20
End If
count40 = Cells(rng.Item(i).Row, 11).Value
If count40 > 1 Then
total = total + count40
End If
count45 = Cells(rng.Item(i).Row, 12).Value
If count45 > 1 Then
total = total + count45
End If
If total <> 0 Then
MsgBox total
End If
Next i
End Sub
【问题讨论】:
-
在第三个示例中,您说需要三行,但您的代码只会找到两行。你能澄清一下吗?我的意思是,在这个例子中 Count20 是 2 并且将被添加到总数中,但 count45 是 1 并且不会被添加。
-
不要相信我的代码...我应该数 3。