I have a piece of code where I sum the cell "K6" from every sheet of the workbook appart the main one called "Data". However it is hard coded and I would like to be able to loop it. Either this or establishing a formula and than extending it. I think it will be easier to see what I'm talking about by looking at the code
This is what I have already
`Sub SumSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Data" Then
SumTotal = SumTotal + ws.Range("K6").Value
SumTotal2 = SumTotal + ws.Range("K7").Value
SumTotal3 = SumTotal + ws.Range("K8").Value
SumTotal4 = SumTotal + ws.Range("K9").Value
End If
Next
Sheets("Data").Range("A6").FormulaR1C1 = SumTotal
Sheets("Data").Range("A7").FormulaR1C1 = SumTotal2
Sheets("Data").Range("A8").FormulaR1C1 = SumTotal3
Sheets("Data").Range("A9").FormulaR1C1 = SumTotal4
End Sub'
This piece of code works like I want it to, but it would be much easier if I could find a way to loop it. Or maybe establish SumTotal as a function and extend it like in regular excel.
Sub SumSheets()
Dim ws As Worksheet
Dim i As Integer
Dim j As Integer
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Data" Then
SumTotal = SumTotal + ws.Range("K6").Value
SumTotal2 = SumTotal + ws.Range("K7").Value
SumTotal3 = SumTotal + ws.Range("K8").Value
SumTotal4 = SumTotal + ws.Range("K9").Value
For i = SumTotal To SumTotal4
For j= 6 To 10
Cells(j,1).Value = i
Next j
Next i
End If
Next
End Sub'
The result with the For Loop I tried is that it only put the value of SumTotal4 in cells 6 through ten. I'm guessing it's because i haven't defined i well enough.