在excel中如何用vba实现选中区域从前往后依次连续累加,满足条件就停止

For Each c In Selection
If s + c - a <= 0 Then
s=s+c
end if
next
在选中区域做连续累加时,用这种方法,累加到大的数字,不会停止运行会直接跳过并累加后面小的数字怎么办?
注:选中区域为某一行的部分连续单元格时,则从左往右依次选中,vba则依次累加

For Each c In Selection
If s + c - a <= 0 Then
s=s+c
else
exit for
end if
next

看见我加的两行了吗?else和exit for

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-25
For Each c In Selection
If s + c > a Then Exit For
s = s + c
Next
相似回答