首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >小计VBScript

小计VBScript
EN

Stack Overflow用户
提问于 2018-06-12 01:37:11
回答 1查看 311关注 0票数 1

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

合计列在第d列或第4列。我遇到的问题是,在带有"...-TOTAL“的行之后,还有更多的行也需要用"...-TOTAL”进行小计。是客户的名字。我试过下面的代码,但我认为它陷入了无限循环,因为脚本不是运行5分钟就结束了,而是根本没有结束。如果您需要更多信息,请让我知道。谢谢!

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-06-12 01:49:54

正如我在评论中指出的,您应该在此处使用For Each循环,以便可以检查每行的第2 (B)列中的“For Each”值。类似于:

代码语言:javascript
复制
'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
Next
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50803282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档