2014-10-15 4 views
0
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 

제 질문은 어떻게 각 열과 행을 합할 수 있습니까? 틈새 행이 있다면? 각 행을 처리하여 합계를 계산하려고합니다. 데이터에 간격이 없으면 작동하지만 행 사이에 간격이 있으면 각 셀을 완전히 처리하지 못합니다.VBA Excel 합계 열 및 행

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` 
+2

이에 대한 VBA를 필요로하는 대신 워크 시트의 합계를 표시하는 어떤 이유? – Bathsheba

답변

0

가장 좋은 방법은 거의 100 % 이것에 대한 VBA를 사용하는 것이 아니라 스프레드 시트 공식 사용되어서는 안된다 : 여기

내가 가진 무엇

=SUM(B2:B4) 

등을

어떤 이유에서 VBA를 100 % 사용해야하는 경우 스프레드 시트 수식은 여전히 ​​올바른 답변 일 수 있습니다.

아래
Application.WorksheetFunction.Sum(Range("B2:B4")) 
0
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