Using Sum in VBA

smartpat19

Board Regular
Joined
Sep 3, 2014
Messages
114
I am running a few checks at the beginning of my macro to insure that the data is correct.

What am I doing wrong? I want to check that the sum of this range is lower than 10 before running the macro.


Code:
Set costrange = wks.Range("H8:H46")        
Set costcheck = WorksheetFunction.Sum(costrange).Value


If costcheck >= 10 Then GoTo Fail

else 

end if

Thank you!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
You only use Set when setting objects (like Workbooks, Worksheets, Ranges, etc).
You do not use set when calculating values, i.e.
Code:
costcheck = WorksheetFunction.Sum(costrange).Value
 
Upvote 0
You only "Set" objects, not values, so try
Code:
Set costrange = wks.Range("H8:H46")        
costcheck = WorksheetFunction.Sum(costrange)


If costcheck >= 10 Then GoTo Fail

else 

end if
 
Upvote 0
You are welcome. Glad we could help!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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