What is a Range

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
For example, if I have Dat= Range("O59:CL59") what have I got ?

I cannot see it in a Watch, nor print Len(Dat).
What can I do with it ?

Can I put it onto another sheet? And could it carry across any attributes (font, back color etc) as well as values ?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
A Range Object represents a cell or multiple cells in your Excel worksheet.
You can't see, print, copy, etc until you advise it to do so.

eg,
Code:
Dat.copy
will do just that.

Basically, Dat is now sitting there waiting for it's next instruction
 
Upvote 0
suggest you google. Such as https://www.excel-easy.com/vba/range-object.html


best practice is to dim range variables such as : dim Dat as excel.range

then to assign to Dat, something ilke : Set Dat = range("O59:CL59")

for objects "Set" is used. Like : Set wks = activesheet
for simple things like integers, "Set" is not used, rather "Let" which is normally omited as it is not essential
so for integers : let i = 1
or normally : i = 1

What do you want to do?
 
Upvote 0
Thanks for the replies.
I don't get "Dat.Copy". Should there be intellisense?
Do I need to Dim Dat as Range? I had Dim Dat as variant (my thinking being variant was anything?)
From Fazza's link there was Range("C3:C4").Value = Range("A1:A2").Value which was useful. I tried it and it copied the values, but not the formatting.
Is is possible to work with ALL attributes, or do they need to be done individually?
 
Upvote 0
Thanks Mark, read the link.
But not sure how to apply your example. Could you show how it might look (with a range) and in a macro ?
 
Upvote 0
Thanks Mark, read the link.
But not sure how to apply your example. Could you show how it might look (with a range) and in a macro ?

PasteSpecial Paste:=xlPasteAll and Copy Destination:= both use all "attributes".

Code:
Sub PasteAllexample()
    Range("A4").Copy
    Range("D4").PasteSpecial Paste:=xlPasteAll
End Sub

Sub ShorthandPasteAllexample()
    Range("A4").Copy
    Range("D4").PasteSpecial
End Sub

Sub Destinationexample()
    Range("A4").Copy Destination:=Range("D4")
End Sub

Sub ShorthandDestinationexample()
    Range("A4").Copy Range("D4")
End Sub
 
Upvote 0
Thanks Mark, having no trouble now pasting & copying Ranges but some other aspects are a bit frustrating.

I want to compare two Ranges (in two Worksheets). I am putting these test into Sheet1 module.
Code:
If Worksheets("Sheet2").Range("O2:AA2").value <> Range("O2:AA2").value then
I tried with and without .Value. Neither worked

I also found Dim as Range isn't straightforward either
Code:
Dim g as Range
g= Range("O2:CL2")
also gives as error (with or without .Value)

Unless you Dim g as Variant.. but then maybe you haven't got a range ?

Code:
Dim g as Variant
Dim h as variant
g = Worksheets("Sheet2").Range("O2:AA2")
h= Range("O2:AA2")
if g <> h

Gave an error on the last line.

If there is some way to compare 2 ranges, what is it ? And would it include colours or fonts as well as values ?
Thanks.
 
Upvote 0
You need to loop though a range consisting of multiple cells for a comparison either on the sheet or in an array.

I also found Dim as Range isn't straightforward either
Dim g as Range
g= Range("O2:CL2")

A Range is an object so you you need the Set keyword...

Code:
Dim g as Range
Set g= Range("O2:CL2")
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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