2017-07-11 2 views
0

いくつかの値を持つスプレッドシートがあり、これらの値を合計したい。ただし、値は異なる列(2A、2B、2Cなど)にあります。Excel-VBAの行を合計する方法

ExcelでVBAを使用すると、行のセルの合計をどのように計算できますか?私が検索したすべてのソリューションは、列の値を計算する方法を示しています。

+0

以下の回答を試しましたか?あなたを援助する時間を費やした人々のためのフィードバック? –

答えて

0

は、以下のコード(コメントとしてコード内の説明を)してみてください:

Option Explicit 

Sub SumRow() 

Dim LastCol As Long 

With Worksheets("Sheet2") ' modify to your sheet's name 
    LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column ' get last column with data in row 2 

    ' put the SUM result in Cell A3 
    .Cells(3, 1) = WorksheetFunction.Sum(.Cells(2, 1), .Cells(2, LastCol)) 
End With 

End Sub 
0

このループは、任意の事前選択された列の範囲を通過し、一緒に行内のすべての値を加算します。

Dim columnCount AS integer 
    Dim total AS long 

    columnCount = 12345 'the amount of column you want to play with 
    total = 0 'the total you are looking for 

    For i= 1 to columnCount 
     total = total + cells(2,i).value 
    Next i ' 
0

行にエラー値がない場合は、行のすべての値を合計します。最後の列を見つける必要はありません。

Debug.Print WorksheetFunction.Sum(Worksheets("Sheet2").Rows(2))

関連する問題