Selecting a Range using .Cells in VBA

purplefox

New Member
Joined
Jul 27, 2010
Messages
18
Hi MrExcel Community,

I have a query about how to write sometyhing in VBE,

As part of a larger Macro I need to be able to sum a range, although the columns and row references of this range are at the outset unknown.

How I imagine doing this is....

Code:
WorksheetFunction.sum (Sheet3.Range(sheet3.Cells(a,b) & ":" & Sheet3.Cells(a,c)))

I have tried this on it's own as a Macro replacing the a, b & c for values for specific cells, and prefixing with "msgbox" so that it should return the sum in a message box but instead I just get Errors.
I have played around trying to use Columns() & rows() but this doesn't work.
If I replace the Range with .Cells for Sheet3.Range("A1:A2") (not the range I'm looking for, just an example) it works fine and sums, but while I know how to define a range such as Range("A" & i & ":B" & i) I know I have substituted the reference for Cells, but I can't remember if I have ever gotten it to work in the past :eek:

Advice Help is appreciated,

Thanks

Andy
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try

Code:
MsgBox WorksheetFunction.Sum(Sheet3.Range(Sheet3.Cells(a, b), Sheet3.Cells(a, c)))
 
Upvote 0
Thanks Peter, You're a star, it worked like a charm.

I knew I had done it before but couldn't for the life of me remember what I needed to do.

Thanks

Andy :biggrin:
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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