Sum A Range Using VBA

Chris101

New Member
Joined
Feb 17, 2023
Messages
28
Office Version
  1. 365
Platform
  1. Windows
I have a PDF invoice that has been converted to excel. In the excel document I look for "Sales Tax" and I copy the sales tax "amounts" and paste them in column "Q" in the order they are found. My issue is that I would like to sum the range from where the data starts Q2 through the last row and place the sum at the top of column "Q2" replacing what is in the cell already.

In the example provided, the result of $16.35 should be placed in Q2. I can clear the values from Q3 down to last row leaving just the result in column Q2.

When I run my code, it seems to sum the range but with the wrong answer. It places $16.00 in Q2 rather than $16.35. I am assuming it is a rounding issue, and if so, i am not sure how to correct that. It could also be a coding issue or both.

I really appreciate any assistance that is provided. I thought this would be a simple task, but nothing seems to come easy for me when writing VBA.

I have placed my code below and a pic of the data for clarity.

Dim i As Long
Dim a As Long
Dim lastrow As Integer

lastrow = wsh2.Cells(Rows.Count, 17).End(xlUp).Row

i = 2
a = 0

For i = 2 To lastrow
a = a + wsh2.Cells(i, 17).Value
Next

wsh2.Range("Q2") = a

Again, any help is greatly appreciated!

Best Regards,
Chris
 

Attachments

  • sum range.png
    sum range.png
    117.4 KB · Views: 16

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You have defined variable 'a' as a long integer. Instead, define it as a double.

Dim a As Long ---> Dim a As Double


(Tip: when posting code, please try to use 'code tags' to format the code

 
Upvote 0
Solution
Riv01,

Your suggestion worked like a charm. Thank you very much for your time and knowldge in helping me complete this task. You have been a big help.

I will do some reading on how the "code tags" work to make it easier to read.

Thank you again!!!

Chris
 
Upvote 0
Dim lastrow As Integer
FWIW, I'd also recommend that you declare this variable as a Long. The number of potential rows available in Excel is greater than the Integer types upper limit.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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