troubleshooting Question

Better way of writing the attached code

Avatar of greg_c
greg_c asked on
Microsoft Excel
2 Comments1 Solution314 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros