Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default 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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default 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






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default 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



.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select and copy a variable range E. F. Excel Programming 4 August 29th 08 11:17 PM
VBA help to copy variable range Eduardo Excel Discussion (Misc queries) 7 August 18th 08 09:16 PM
Copy a Variable range mathel Excel Programming 5 November 2nd 07 12:17 PM
Copy Variable Range to New Worksheet nospaminlich Excel Programming 3 August 3rd 07 01:20 AM
Macro to copy a specified range to a variable range SWT Excel Programming 4 October 21st 05 08:24 PM


All times are GMT +1. The time now is 05:07 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"