Need VBA to sum up a row that could vary in length

kac1125

Board Regular
Joined
Jul 31, 2014
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Trying to run a VBA that will sum up a column that could be any number in length. So I need it to search for the first empty cell and put the sum there. I can only get it to work as follows:

Range("K4").Select
Selection.End(xlDown).Select
Range("K19").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=SUM(R[-15]C:R[-1]C)"
Range("K20").Select

So I am trying to sum up K4:last cell k20 happened to be the last cell in my current file. But want it to be able to work if there were more rows as well.

Please let me know if you have any ideas. Thanks in advance!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi. Try this:

Code:
Sub InsertFormula()
 Dim LastRow As Long
  LastRow = Cells(Rows.Count, "K").End(3).Row
  Cells(LastRow + 1, "K") = "=SUM(k4:k" & LastRow & ")"
End Sub
 
Upvote 0
Maybe this:

Code:
range("K"& Range("K4").end(xldown).row +1).formula = "=sum(k4:k" & Range("K4").end(xldown).row & ")"
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top