Hi I have been trying to get the total sum of unknown number of rows. However i could not get it to work from my code below. Any advice of how to get it work?
Private Sub CmdNextSection_Click()
lastrow = ActiveSheet.Range("b" & Rows.Count).End(xlUp).Row
With Cells(lastrow + 1, 5)
.Value = "Subtotal"
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
With Cells(lastrow + 2, 5)
.Value = "GST"
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
With Cells(lastrow + 3, 5)
.Value = "Grand Total"
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
Subtotal = 0
For i = 19 To lastrow
With Cells(lastrow + 1, 6)
.Value = Subtotal + Range("i,6")
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
Next i
End Sub
Private Sub CmdNextSection_Click()
lastrow = ActiveSheet.Range("b" & Rows.Count).End(xlUp).Row
With Cells(lastrow + 1, 5)
.Value = "Subtotal"
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
With Cells(lastrow + 2, 5)
.Value = "GST"
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
With Cells(lastrow + 3, 5)
.Value = "Grand Total"
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
Subtotal = 0
For i = 19 To lastrow
With Cells(lastrow + 1, 6)
.Value = Subtotal + Range("i,6")
.WrapText = False
.Font.Bold = False
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlRight
.Font.Bold = True
End With
Next i
End Sub