Excel查找循环中每第6列的总和

时间:2017-08-20 02:10:27

标签: excel vb.net

我有一个excel表格,我已经通过宏插入了空白列,但现在我需要找到每6列的总和并将值存储在空列中?试图从7到7步,但它不起作用。 我需要空列来获得最后6列的平均值

enter image description here

我试过的代码是

Sub sum_of_every_6th_column() 
    Dim iLastCol as Integer 
    iLastCol = Cells(1, Columns.Count).End(xlToLeft).Column ' same as CTRL+RIGHT ARROW 
    For colx = 7 To iLastCol Step 8 '?? Unable to understand what can come here 
    Next 
End Sub

1 个答案:

答案 0 :(得分:1)

尝试此代码,根据您的需要更改

Sub sumCols()
Dim i As Long, j As Long, k As Long
j = 0
For k = 1 To Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To Cells(k, Columns.Count).End(xlToLeft).Column + 1
    If IsEmpty(Cells(k, i)) Then
        Cells(k, i) = j
        j = 0
    Else
        j = j + Cells(k, i)
    End If
Next i
Next k
End Sub