Sum cell range on specified worksheet

sricketts

New Member
Joined
Aug 21, 2009
Messages
7
I have the following line of VBA code that sums a range of cells on the active worksheet. I need to change that to sum on a specified worksheet. Is there a way to do that? I thought it would be simple, but have had no luck... Actually using variables for the cells, but simplified here for example's sake.

If Application.WorksheetFunction.Sum(Range(Cells(2, 2), Cells(123, 2))) = 0
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try Using:

If Sheets("your worksheet name").Range("A2,B3,F10....etc")

changing the red text to your own sheet and range.

Hope it helps,

Lee.
 
Upvote 0
I have the following line of VBA code that sums a range of cells on the active worksheet. I need to change that to sum on a specified worksheet. Is there a way to do that? I thought it would be simple, but have had no luck... Actually using variables for the cells, but simplified here for example's sake.

If Application.WorksheetFunction.Sum(Range(Cells(2, 2), Cells(123, 2))) = 0

Application.WorksheetFunction.Sum(Sheets("Sheet1").Range(Cells(2, 2), Cells(123, 2)))
 
Upvote 0
Thanks for the suggestion. I coded it this way then:

If Sheets("data").Range(Cells(2, 2), Cells(123, 2)) = 0 Then ...


which returned a run-time error 1004, "application-defined or object-defined error".
 
Upvote 0
sricketts,

Try:

Code:
If Application.WorksheetFunction.Sum(Worksheets("Sheet1").Range(Cells(2, 2), Cells(123, 2))) = 0 Then
 
Upvote 0
Thanks Travis. Tried this:

If Application.WorksheetFunction.Sum(Sheets("data").Range(Cells(2, 2), Cells(123, 2))) = 0 Then


...and got the same mentioned runtime error.
 
Upvote 0
Go to the immediate window and try
? Application.WorksheetFunction.Sum(Worksheets("data").Range(Cells(2, 2), Cells(123, 2)))

all on one line. This will at least tell you if you can get an answer from this sum.
 
Upvote 0
Code:
If WorksheetFunction.Sum(Range(Sheets("data").Cells(2, 2), Sheets("data").Cells(123, 2))) = 0 Then
 
Upvote 0
Hi

Remark:

In a range statement

Code:
Range(Cells(),Cells())

all the range objects must be in the same worksheet or else you get an error

These are wrong:

Code:
Worksheets("Data").Range(Cells(),Cells())
Range(Worksheets("Data").Cells(),Cells())
Worksheets("Data").Range(Worksheets("Data").Cells(),Cells())
etc

This is right:

Code:
Result = WorksheetFunction.Sum(Worksheets("Data").Range(Worksheets("Data").Cells(2, 2), Worksheets("Data").Cells(123, 2)))

Or, easier to write and to read:

Code:
With Worksheets("Data")
    Resut = WorksheetFunction.Sum(.Range(.Cells(2, 2), .Cells(123, 2)))
End With

HTH
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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