求大神帮忙,多列单元格求和用vba如何设置
vba吧
全部回复
仅看楼主
level 2
Lin1BC 楼主
之前用函数设置但是求和的单元格太多了,导致整个表格反应很慢。
设置检查sheet"数据"b3:b不为空值时,求和以下的列。
q=BA+BQ-AS-BI-BY+CG
R=BB+BR-AT-BJ-BZ+CH
S=BC+BS-AU-BK-CA+CI
T=BD+BT-AV-BL-CB+CJ
U=BE+BU-AW-BM-CC+CK
V=BF+BV-AX-BN-CD+CL
W=BG+BW-AY-BO-CE+CM
X=P:W
Y=,J*X
2024年11月13日 05点11分 1
level 4
Sub CalculateBasedOnConditions()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("数据") '设置要操作的工作表,根据实际修改工作表名称
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row '找到B列最后一个有数据的行
Dim i As Long
For i = 3 To lastRow '从第3行开始遍历到B列最后有数据的行
If Not IsEmpty(ws.Cells(i, "B")) Then
Dim q As Double
Dim r As Double
Dim s As Double
Dim t As Double
Dim u As Double
Dim v As Double
Dim w As Double
Dim x As Double
Dim y As Double
q = ws.Cells(i, "BA") + ws.Cells(i, "BQ") - ws.Cells(i, "AS") - ws.Cells(i, "BI") - ws.Cells(i, "BY") + ws.Cells(i, "CG")
r = ws.Cells(i, "BB") + ws.Cells(i, "BR") - ws.Cells(i, "AT") - ws.Cells(i, "BJ") - ws.Cells(i, "BZ") + ws.Cells(i, "CH")
s = ws.Cells(i, "BC") + ws.Cells(i, "BS") - ws.Cells(i, "AU") - ws.Cells(i, "BK") - ws.Cells(i, "CA") + ws.Cells(i, "CI")
t = ws.Cells(i, "BD") + ws.Cells(i, "BT") - ws.Cells(i, "AV") - ws.Cells(i, "BL") - ws.Cells(i, "CB") + ws.Cells(i, "CJ")
u = ws.Cells(i, "BE") + ws.Cells(i, "BU") - ws.Cells(i, "AW") - ws.Cells(i, "BM") - ws.Cells(i, "CC") + ws.Cells(i, "CK")
v = ws.Cells(i, "BF") + ws.Cells(i, "BV") - ws.Cells(i, "AX") - ws.Cells(i, "BN") - ws.Cells(i, "CD") + ws.Cells(i, "CL")
w = ws.Cells(i, "BG") + ws.Cells(i, "BW") - ws.Cells(i, "AY") - ws.Cells(i, "BO") - ws.Cells(i, "CE") + ws.Cells(i, "CM")
x = q + r + s + t + u + v + w
y = ws.Cells(i, "J") * x
ws.Cells(i, "P") = q
ws.Cells(i, "Q") = r
ws.Cells(i, "R") = s
ws.Cells(i, "S") = t
ws.Cells(i, "T") = u
ws.Cells(i, "U") = v
ws.Cells(i, "V") = w
ws.Cells(i, "W") = x
ws.Cells(i, "X") = y
End If
Next i
End Sub
2024年11月15日 14点11分 3
1