我有一个类似下面的excel电子表格(我将所有机密信息设置为白色字体,所以忽略空格)

合计列在第d列或第4列。我遇到的问题是,在带有"...-TOTAL“的行之后,还有更多的行也需要用"...-TOTAL”进行小计。是客户的名字。我试过下面的代码,但我认为它陷入了无限循环,因为脚本不是运行5分钟就结束了,而是根本没有结束。如果您需要更多信息,请让我知道。谢谢!
Do Until InStr(objExcel.Cells(c,2).Value, "TOTAL")
total = total + objExcel.Cells(e,4).Value
if InStr(objExcel.Cells(c,2).Value, "TOTAL") then
objExcel.Cells(c,4).Value = total
total = 0
end if
Loop发布于 2018-06-12 01:49:54
正如我在评论中指出的,您应该在此处使用For Each循环,以便可以检查每行的第2 (B)列中的“For Each”值。类似于:
'Loop through each cell in column B
For Each rngCell in Range("B:B").Cells
'Check to see if it contains "TOTAL" as part of it's value
If Instr(rngCell.value, "TOTAL") Then
'Set the value of the cell 2 columns over (Column D) and exit loop
rngCell.Offset(,2).value = total
Exit For
End If
'Collect and increment the total
total = total + rngCell.Offset(,2).value
Nexthttps://stackoverflow.com/questions/50803282
复制相似问题