Refer to a range using cells instead of Range

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This works:

Rich (BB code):
MaxVal = Application.WorksheetFunction.Max(Sheet1.Range("A1:F1"))

but not this:

Rich (BB code):
MaxVal = Application.WorksheetFunction.Max(Range(Cells(1,1), Cells(1,6))))

neither do these:

Rich (BB code):
Rich (BB code):
Rich (BB code):
MaxVal = Application.WorksheetFunction.Max(Range(Sheet1.Cells(1,1), Sheet1.Cells(1,6))))
MaxVal = Application.WorksheetFunction.Max(Sheet1.Range(Cells(1,1), Cells(1,6))))
MaxVal = Application.WorksheetFunction.Max(Sheet1.Range(Sheet1.Cells(1,1), Sheet1.Cells(1,6))))

Why is that?

Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Well you have one too many bracket but aside from that they all work if you are on Sheet1 and the first and last in the group of 3 does if you arent.
 
Upvote 0
This would work
Code:
[COLOR=#222222][FONT=Verdana]MaxVal = Application.WorksheetFunction.Max(Sheet1.Range(Sheet1.Cells(1,1), Sheet1.Cells(1,6))))[/FONT][/COLOR]
except you have 1 to many ) at the end
 
Upvote 0
Why is that?
All the failing ones have syntax errors (an extra closing parenthesis at the end)

Even after that correction, at least one will fail if Sheet1 is not the active sheet because an un-referenced range will apply to the active sheet.
 
Upvote 0
Thanks to all.

My objective is NOT to be on Sheet1 when the code runs.

Agree the last in the set of three works but not the first.

Not a problem, as long as I've got one that does work.
 
Last edited:
Upvote 0
This one doesnt?

Code:
MaxVal = Application.WorksheetFunction.Max(Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, 6)))

It does on my machine even if Sheet1 isnt the active sheet.
 
Upvote 0
This one doesnt?

Code:
MaxVal = Application.WorksheetFunction.Max(Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, 6)))

It does on my machine even if Sheet1 isnt the active sheet.

That should work as long as the code is not in another worksheet's code module. You should really qualify the Range property too though.

In a worksheet code module, Range or Cells with no qualifier is equivalent to Me.Range and Me.Cells; anywhere else, it is equivalent to Application.Range or Application.Cells
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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