Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将vba中两个空单元格之间列中的所有值相加?_Vba_Excel_Sum - Fatal编程技术网

如何将vba中两个空单元格之间列中的所有值相加?

如何将vba中两个空单元格之间列中的所有值相加?,vba,excel,sum,Vba,Excel,Sum,我是Excel VBA的新手,所以我非常感谢您的帮助。 我在Excel VBA中有以下代码,它在特定条件下过滤sheetdata中的数据后,在sheetoutput中创建两个单独的表 Dim i, LastRow LastRow = Sheets("data").Range("B" & Rows.Count).End(xlUp).Row Sheets("output").Range("A2:L500").ClearContents Sheets("output").Ran

我是Excel VBA的新手,所以我非常感谢您的帮助。 我在Excel VBA中有以下代码,它在特定条件下过滤sheetdata中的数据后,在sheetoutput中创建两个单独的表

 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
尺寸i,最后一行
LastRow=工作表(“数据”)。范围(“B”和行数。计数)。结束(xlUp)。行
纸张(“输出”)。范围(“A2:L500”)。清晰内容
工作表(“输出”)。范围(“A”和行数)。结束(xlUp)。偏移量(1)=“第一个表”
对于i=2到最后一行

如果工作表(“数据”).单元格(i,“G”).值>0和工作表(“数据”).单元格(i,“G”).值2和工作表(“数据”).单元格(i,“G”).值无论您的表之间有一个空格还是五十个空格,此解决方案都应有效:

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
Sub-SumTablesByColumn(sCol作为字符串)
昏暗的灯塔一样长
暗x等长
昏暗的柜台一样长
暗淡的一排像长的一样
lStartRow=1
计数器=1
lLastRow=ActiveSheet.Range(sCol&500000)。End(xlUp)。行
对于x=1到lLastRow+1
如果范围(sCol&x).Value“”和范围(sCol&x+1).Value“”,则
计数器=计数器+1
ElseIf Range(sCol&x).Value“”和Range(sCol&x+1).Value=”“然后
范围(sCol&counter+1)。公式=“=SUM(&sCol&lStartRow&“:”&sCol&counter&“)”
x=x+1
如果范围(sCol&x+1)。值为“”,则
lStartRow=x+1
计数器=x+1
如果结束
ElseIf范围(sCol&x).Value=”“和范围(sCol&x+1).Value“”然后
lStartRow=x+1
计数器=x+1
如果结束
下一个
端接头