如何对 vba 中两个空单元格之间列中的所有值求和?
how to sum all values in column between two empty cells in vba?
我是 Excel VBA 的新手,非常感谢您的帮助。
我在 Excel VBA 中有以下代码,它在过滤 [=] 中的数据后在 sheet output 中创建两个单独的 tables 22=] data 有一定条件.
Dim i, LastRow
LastRow = Sheets("data").Range("B" & Rows.Count).End(xlUp).Row
Sheets("output").Range("A2:L500").ClearContents
Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(1) = "first table"
For i = 2 To LastRow
If Sheets("data").Cells(i, "G").Value > 0 And Sheets("data").Cells(i, "G").Value <= 2 Then
Sheets("data").Cells(i, "G").EntireRow.Copy Destination:=Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(3) = "second table"
For i = 2 To LastRow
If Sheets("data").Cells(i, "G").Value > 2 And Sheets("data").Cells(i, "G").Value <= 3 Then
Sheets("data").Cells(i, "G").EntireRow.Copy Destination:=Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End Sub
我想要它做的是为每个table(分别为table 1和[=24分别给出某些特定列(例如,C列)的所有值的总和=] 2).
无论您的表之间有一个 space 还是五十个 space,此解决方案都应该有效:
Sub SumTablesByColumn(sCol As String)
Dim lLastRow As Long
Dim x As Long
Dim counter As Long
Dim lStartRow As Long
lStartRow = 1
counter = 1
lLastRow = ActiveSheet.Range(sCol & 500000).End(xlUp).Row
For x = 1 To lLastRow + 1
If Range(sCol & x).Value <> "" And Range(sCol & x + 1).Value <> "" Then
counter = counter + 1
ElseIf Range(sCol & x).Value <> "" And Range(sCol & x + 1).Value = "" Then
Range(sCol & counter + 1).Formula = "=SUM(" & sCol & lStartRow & ":" & sCol & counter & ")"
x = x + 1
If Range(sCol & x + 1).Value <> "" Then
lStartRow = x + 1
counter = x + 1
End If
ElseIf Range(sCol & x).Value = "" And Range(sCol & x + 1).Value <> "" Then
lStartRow = x + 1
counter = x + 1
End If
Next
End Sub
我是 Excel VBA 的新手,非常感谢您的帮助。 我在 Excel VBA 中有以下代码,它在过滤 [=] 中的数据后在 sheet output 中创建两个单独的 tables 22=] data 有一定条件.
Dim i, LastRow
LastRow = Sheets("data").Range("B" & Rows.Count).End(xlUp).Row
Sheets("output").Range("A2:L500").ClearContents
Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(1) = "first table"
For i = 2 To LastRow
If Sheets("data").Cells(i, "G").Value > 0 And Sheets("data").Cells(i, "G").Value <= 2 Then
Sheets("data").Cells(i, "G").EntireRow.Copy Destination:=Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(3) = "second table"
For i = 2 To LastRow
If Sheets("data").Cells(i, "G").Value > 2 And Sheets("data").Cells(i, "G").Value <= 3 Then
Sheets("data").Cells(i, "G").EntireRow.Copy Destination:=Sheets("output").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next i
End Sub
我想要它做的是为每个table(分别为table 1和[=24分别给出某些特定列(例如,C列)的所有值的总和=] 2).
无论您的表之间有一个 space 还是五十个 space,此解决方案都应该有效:
Sub SumTablesByColumn(sCol As String)
Dim lLastRow As Long
Dim x As Long
Dim counter As Long
Dim lStartRow As Long
lStartRow = 1
counter = 1
lLastRow = ActiveSheet.Range(sCol & 500000).End(xlUp).Row
For x = 1 To lLastRow + 1
If Range(sCol & x).Value <> "" And Range(sCol & x + 1).Value <> "" Then
counter = counter + 1
ElseIf Range(sCol & x).Value <> "" And Range(sCol & x + 1).Value = "" Then
Range(sCol & counter + 1).Formula = "=SUM(" & sCol & lStartRow & ":" & sCol & counter & ")"
x = x + 1
If Range(sCol & x + 1).Value <> "" Then
lStartRow = x + 1
counter = x + 1
End If
ElseIf Range(sCol & x).Value = "" And Range(sCol & x + 1).Value <> "" Then
lStartRow = x + 1
counter = x + 1
End If
Next
End Sub