Sql VBA Excel列和行总和

Sql VBA Excel列和行总和,sql,vba,excel,Sql,Vba,Excel,我的问题是如何对每一列和每一行求和?如果有空位行?我试图处理每一行,使其总和。如果数据中没有间隙,它就可以工作,但是如果行之间有间隙,它就不能完全处理每个单元格 以下是我所拥有的: ABC ASD BHP WER THY SUM 1 789 564 654 546 654 3207 2 103 123 213 123 654 1216 3 546 394 879 654 654 2733 您最好的选择是几乎100%不使用VBA,而是使用电子表格公式: Su

我的问题是如何对每一列和每一行求和?如果有空位行?我试图处理每一行,使其总和。如果数据中没有间隙,它就可以工作,但是如果行之间有间隙,它就不能完全处理每个单元格

以下是我所拥有的:

    ABC ASD BHP WER THY SUM    
1   789 564 654 546 654 3207

2   103 123 213 123 654 1216    
3   546 394 879 654 654 2733

您最好的选择是几乎100%不使用VBA,而是使用电子表格公式:

Sub test()

Dim intRowTot As Integer
Dim intRowCnt As Integer
Dim intColTot As Integer
Dim intColCnt As Integer

Range("B2").Select
Do Until IsEmpty(ActiveCell)

    intRowTot = 0
    intColTot = 0

    Do Until IsEmpty(ActiveCell)
        intRowTot = intRowTot + 1
        intColTot = intColTot + ActiveCell.Value
        intColCnt = intColCnt + 1
        ActiveCell.Offset(1, 0).Select

    Loop
    ActiveCell.Offset(1, 0).Value = intColTot
    ActiveCell.Offset(-intRowTot, 1).Select
    Loop

Range("B2").Select
Do Until IsEmpty(ActiveCell)

    intRowTot = 0
    intColTot = 0

    Do Until IsEmpty(ActiveCell)
        intRowTot = intRowTot + ActiveCell.Value
        intColTot = intColTot + 1
        intRowCnt = intRowCnt + 1
        ActiveCell.Offset(0, 1).Select
    Loop

    ActiveCell.Offset(0, 1).Value = intRowTot
    ActiveCell.Offset(1, -intColTot).Select
    Loop
End Sub`
等等

如果出于某种原因,您100%需要使用VBA,电子表格公式仍然可能是正确的答案:

=SUM(B2:B4)
上表,代码如下:

ABC ASD BHP WER THY    
789 564 654 546 654

103 123 213 123 654   
546 394 879 654 654

为什么需要VBA而不是在工作表上显示总和?
ABC ASD BHP WER THY    
789 564 654 546 654

103 123 213 123 654   
546 394 879 654 654
Sub test()


Dim i As Integer
Dim j As Integer
Dim tempsum As Long
'columns
For i = 1 To 5
    tempsum = 0
    For j = 1 To 5
        If IsNumeric(Cells(j, i).Value) = True Then
            tempsum = tempsum + Cells(j, i).Value
        End If
    Next
    If tempsum > 0 Then
        Cells(j, i).Value = tempsum
    End If
Next


'rows
For i = 1 To 5
    tempsum = 0
    For j = 1 To 5
        If IsNumeric(Cells(i, j).Value) = True Then
            tempsum = tempsum + Cells(i, j).Value
        End If
    Next
    If tempsum > 0 Then
        Cells(i, j).Value = tempsum
    End If
Next



End Sub