Avatar of greg_c
greg_c
 asked on

Better way of writing the attached code

Hi

I've written the code below on the run, and was wondering if there is a better way of writing it.

Sub add_Totals()
Dim lstRow As Long
Dim ws As Worksheet
Dim ws2 As Worksheet
Set ws = Sheets("OriginalData")
Set ws2 = Sheets("Admended")
lstRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row
Worksheets("Title").Range("B16").Formula = "=SUM(originalData!C2:Q2" & lstRow & ")"
Worksheets("Title").Range("B19").Formula = "=SUM(Admended!C2:Q2" & lstRow & ")"
lstRow = ws.Cells(ws.Rows.Count, "R").End(xlUp).Row
Worksheets("Title").Range("C16").Formula = "=SUM(originalData!R2:AC2" & lstRow & ")"
lstRow = ws.Cells(ws.Rows.Count, "AD").End(xlUp).Row
Worksheets("Title").Range("D16").Formula = "=SUM(originalData!AD2:AO2" & lstRow & ")"
lstRow = ws.Cells(ws.Rows.Count, "AP").End(xlUp).Row
Worksheets("Title").Range("E16").Formula = "=SUM(originalData!AP2:BA2" & lstRow & ")"
Worksheets("Title").Range("C19").Formula = "=SUM(Admended!R2:AC2" & lstRow & ")"
Worksheets("Title").Range("D19").Formula = "=SUM(Admended!AD2:AO2" & lstRow & ")"
Worksheets("Title").Range("E19").Formula = "=SUM(Admended!AP2:BA2" & lstRow & ")"
End Sub

Thanks

Greg
Microsoft Excel

Avatar of undefined
Last Comment
greg_c

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rory Archibald

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
greg_c

ASKER
Thanks rorya.  I'm very grateful for your help.
Your help has saved me hundreds of hours of internet surfing.
fblack61