ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   copy range to a variable. (https://www.excelbanter.com/excel-programming/438928-copy-range-variable.html)

Brian S[_2_]

copy range to a variable.
 
Is it possible to copy a range of cells and set them equal to a variable?
Something like this:
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
x = selection


Thanks

Gary Keramidas

copy range to a variable.
 
this should be all you need

rng = Range("A1", range("A1").End(xlToRight))

open the immediate window in the vb editor (control G) and paste the above line and
press enter

then enter this line and press enter and it should display the range
?rng.address


--


Gary Keramidas
Excel 2003


"Brian S" wrote in message
...
Is it possible to copy a range of cells and set them equal to a variable?
Something like this:
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
x = selection


Thanks



Jacob Skaria

copy range to a variable.
 
You can set them to a Range variable as below

Dim myRange As Range
Set myRange = Range("A1").Resize(Range("A1").End(xlDown).Row, _
Range("A1").End(xlToRight).Column)

myRange.Select


--
Jacob


"Brian S" wrote:

Is it possible to copy a range of cells and set them equal to a variable?
Something like this:
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
x = selection


Thanks


Gary Keramidas

copy range to a variable.
 
sorry missed the 2nd part of your selection

--


Gary Keramidas
Excel 2003


"Gary Keramidas" <GKeramidasAtMSN.com wrote in message
...
this should be all you need

rng = Range("A1", range("A1").End(xlToRight))

open the immediate window in the vb editor (control G) and paste the above line
and press enter

then enter this line and press enter and it should display the range
?rng.address


--


Gary Keramidas
Excel 2003


"Brian S" wrote in message
...
Is it possible to copy a range of cells and set them equal to a variable?
Something like this:
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
x = selection


Thanks




Gary Keramidas

copy range to a variable.
 
here is another way, jacob's is shorter

Dim rng2 As Range

Set rng2 = Range(Range("A1", Range("A1").End(xlDown)),
Range(Range("A1").End(xlToRight), _
Range("A1").End(xlToRight).End(xlDown)))
rng2.Select

--


Gary Keramidas
Excel 2003


"Gary Keramidas" <GKeramidasAtMSN.com wrote in message
...
sorry missed the 2nd part of your selection

--


Gary Keramidas
Excel 2003


"Gary Keramidas" <GKeramidasAtMSN.com wrote in message
...
this should be all you need

rng = Range("A1", range("A1").End(xlToRight))

open the immediate window in the vb editor (control G) and paste the above line
and press enter

then enter this line and press enter and it should display the range
?rng.address


--


Gary Keramidas
Excel 2003


"Brian S" wrote in message
...
Is it possible to copy a range of cells and set them equal to a variable?
Something like this:
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
x = selection


Thanks





Rick Rothstein

copy range to a variable.
 
Since you are using xlDown and xlToRight, wouldn't this be equivalent to the
Set statement you posted?

Set myRange = Range("A1").CurrentRegion

--
Rick (MVP - Excel)


"Jacob Skaria" wrote in message
...
You can set them to a Range variable as below

Dim myRange As Range
Set myRange = Range("A1").Resize(Range("A1").End(xlDown).Row, _
Range("A1").End(xlToRight).Column)

myRange.Select


--
Jacob


"Brian S" wrote:

Is it possible to copy a range of cells and set them equal to a variable?
Something like this:
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
x = selection


Thanks



Jacob Skaria

copy range to a variable.
 
Gary, even this might be what the OP is looking for ()..

Activesheet.usedrange.select

OR

Dim myRange as Range
Set myRange = ActiveSheet.UsedRange

--
Jacob


"Gary Keramidas" wrote:

here is another way, jacob's is shorter

Dim rng2 As Range

Set rng2 = Range(Range("A1", Range("A1").End(xlDown)),
Range(Range("A1").End(xlToRight), _
Range("A1").End(xlToRight).End(xlDown)))
rng2.Select

--


Gary Keramidas
Excel 2003


"Gary Keramidas" <GKeramidasAtMSN.com wrote in message
...
sorry missed the 2nd part of your selection

--


Gary Keramidas
Excel 2003


"Gary Keramidas" <GKeramidasAtMSN.com wrote in message
...
this should be all you need

rng = Range("A1", range("A1").End(xlToRight))

open the immediate window in the vb editor (control G) and paste the above line
and press enter

then enter this line and press enter and it should display the range
?rng.address


--


Gary Keramidas
Excel 2003


"Brian S" wrote in message
...
Is it possible to copy a range of cells and set them equal to a variable?
Something like this:
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
x = selection


Thanks



.



All times are GMT +1. The time now is 08:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com