VBA计算A列含有合并单元格
发布网友
发布时间:2023-07-19 22:12
我来回答
共2个回答
热心网友
时间:2024-04-15 13:58
没系统的看过书,不知道合并单元格怎么搞,不过我知道合并单元格的值是在第一个单元格里的,所以遍历A列,
不是空格
记录行数(用来填答案)
重置求和数为所在行的B列
是空格,把数据加到求和数里
所在行的B列加到求和数里
把答案铁道记录行数的D列
写成代码大概就是
sub demo()
dim rowNum as int,sum as double,rng as range
for each rng in range([A2],[b2].end(xldown).offset(0,-1))
if rng=empty then
sum=sum+rng.offset(0,2)
else
if rowNum>0 then
cells(rowNum,5)=sum
end if
sum=rng.offset(0,2)
rowNum=rng.row()
end if
next rng
cells(rowNum,5)=sum
end sub
追问提示出错了
追答
谢谢你的追问 那我调试一下
Sub demo()
Dim rowNum As Integer, sum As Double, rng As Range
For Each rng In Range([A2], [b2].End(xlDown).Offset(0, -1))
If rng = Empty Then
sum = sum + rng.Offset(0, 2)
Else
If rowNum > 0 Then
Cells(rowNum, 5) = sum
End If
sum = rng.Offset(0, 2)
rowNum = rng.Row()
End If
Next rng
Cells(rowNum, 5) = sum
End Sub
热心网友
时间:2024-04-15 13:58
Sub s()
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(i, 1).MergeCells Then
r = Cells(i, 1).MergeArea.Rows.Count - 1
sm = 0
For j = 0 To r
sm = sm + Cells(i + j, 3)
Next
Cells(i, 5) = sm
i = i + r
Else
Cells(i, 5) = Cells(i, 3)
End If
Next
End Sub
来自:求助得到的回答