Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Excel Range Variables

My problem is trying to use a range variable with the Cells object. See the
code sample code below. I am trying to copy a group of cells from a sheet
that is not active. it works if I reference one cell but not more than one.
I can't seem to get the syntax right. hel would be appreciated.

Sub UseARangeVariable()

Dim MyRange As Range

Sheets("Rota").Select
Range("A10").Select
Set MyRange = ActiveCell.CurrentRegion

Sheets("Sheet1").Select 'Change Sheets

MyRange.Cells(1, 1).Copy 'Refer to first sheet i.e. Rota
Range("a1").Select
ActiveSheet.Paste 'But Paste to sheet1...this works

MyRange.Range(Cells(1, 2), Cells(1, 7)).Copy 'Try to do same with cell range
Range("a2").Select 'but causes 1004 error
ActiveSheet.Paste
End Sub
--
Alan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Excel Range Variables

MyRange.Range(MyRange.Cells(1, 2), MyRange.Cells(1, 7))

unqualified Cells refers to the active sheet.

--
Regards,
Tom Ogilvy

"Alan" wrote in message
...
My problem is trying to use a range variable with the Cells object. See

the
code sample code below. I am trying to copy a group of cells from a sheet
that is not active. it works if I reference one cell but not more than

one.
I can't seem to get the syntax right. hel would be appreciated.

Sub UseARangeVariable()

Dim MyRange As Range

Sheets("Rota").Select
Range("A10").Select
Set MyRange = ActiveCell.CurrentRegion

Sheets("Sheet1").Select 'Change Sheets

MyRange.Cells(1, 1).Copy 'Refer to first sheet i.e. Rota
Range("a1").Select
ActiveSheet.Paste 'But Paste to sheet1...this works

MyRange.Range(Cells(1, 2), Cells(1, 7)).Copy 'Try to do same with cell

range
Range("a2").Select 'but causes 1004 error
ActiveSheet.Paste
End Sub
--
Alan



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Excel Range Variables

Range(MyRange(1,2),MyRange(1,7)).Copy

Alan Beban

Alan wrote:
My problem is trying to use a range variable with the Cells object. See the
code sample code below. I am trying to copy a group of cells from a sheet
that is not active. it works if I reference one cell but not more than one.
I can't seem to get the syntax right. hel would be appreciated.

Sub UseARangeVariable()

Dim MyRange As Range

Sheets("Rota").Select
Range("A10").Select
Set MyRange = ActiveCell.CurrentRegion

Sheets("Sheet1").Select 'Change Sheets

MyRange.Cells(1, 1).Copy 'Refer to first sheet i.e. Rota
Range("a1").Select
ActiveSheet.Paste 'But Paste to sheet1...this works

MyRange.Range(Cells(1, 2), Cells(1, 7)).Copy 'Try to do same with cell range
Range("a2").Select 'but causes 1004 error
ActiveSheet.Paste
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Excel Range Variables

Many Thanks Alan Beban...exactly what I needed

"Alan" wrote:

My problem is trying to use a range variable with the Cells object. See the
code sample code below. I am trying to copy a group of cells from a sheet
that is not active. it works if I reference one cell but not more than one.
I can't seem to get the syntax right. hel would be appreciated.

Sub UseARangeVariable()

Dim MyRange As Range

Sheets("Rota").Select
Range("A10").Select
Set MyRange = ActiveCell.CurrentRegion

Sheets("Sheet1").Select 'Change Sheets

MyRange.Cells(1, 1).Copy 'Refer to first sheet i.e. Rota
Range("a1").Select
ActiveSheet.Paste 'But Paste to sheet1...this works

MyRange.Range(Cells(1, 2), Cells(1, 7)).Copy 'Try to do same with cell range
Range("a2").Select 'but causes 1004 error
ActiveSheet.Paste
End Sub
--
Alan

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Excel Range Variables

Alan wrote:
Many Thanks Alan Beban...exactly what I needed


You're welcome. The general lesson is that once the range has been
assigned to an object variable (i.e., in this case MyArray), the Cells
Method is superfluous; the object variable carries its own qualification
with it.

Alan Beban

"Alan" wrote:


My problem is trying to use a range variable with the Cells object. See the
code sample code below. I am trying to copy a group of cells from a sheet
that is not active. it works if I reference one cell but not more than one.
I can't seem to get the syntax right. hel would be appreciated.

Sub UseARangeVariable()

Dim MyRange As Range

Sheets("Rota").Select
Range("A10").Select
Set MyRange = ActiveCell.CurrentRegion

Sheets("Sheet1").Select 'Change Sheets

MyRange.Cells(1, 1).Copy 'Refer to first sheet i.e. Rota
Range("a1").Select
ActiveSheet.Paste 'But Paste to sheet1...this works

MyRange.Range(Cells(1, 2), Cells(1, 7)).Copy 'Try to do same with cell range
Range("a2").Select 'but causes 1004 error
ActiveSheet.Paste
End Sub
--
Alan



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Excel Range Variables

the Cells Method is superfluous


with noted exceptions.

Set MyRange = Range("A1:A10").Columns
? Range(MyRange(1,2),MyRange(1,7)).address '<== Raises an error
? MyRange.Range(MyRange.Cells(1, 2), MyRange.Cells(1, 7)).Address
$B$1:$G$1


--
Regards,
Tom Ogilvy


"Alan Beban" wrote in message
...
Alan wrote:
Many Thanks Alan Beban...exactly what I needed


You're welcome. The general lesson is that once the range has been
assigned to an object variable (i.e., in this case MyArray), the Cells
Method is superfluous; the object variable carries its own qualification
with it.

Alan Beban

"Alan" wrote:


My problem is trying to use a range variable with the Cells object. See

the
code sample code below. I am trying to copy a group of cells from a

sheet
that is not active. it works if I reference one cell but not more than

one.
I can't seem to get the syntax right. hel would be appreciated.

Sub UseARangeVariable()

Dim MyRange As Range

Sheets("Rota").Select
Range("A10").Select
Set MyRange = ActiveCell.CurrentRegion

Sheets("Sheet1").Select 'Change Sheets

MyRange.Cells(1, 1).Copy 'Refer to first sheet i.e. Rota
Range("a1").Select
ActiveSheet.Paste 'But Paste to sheet1...this works

MyRange.Range(Cells(1, 2), Cells(1, 7)).Copy 'Try to do same with cell

range
Range("a2").Select 'but causes 1004 error
ActiveSheet.Paste
End Sub
--
Alan



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Excel Range Variables

Tom Ogilvy wrote:
the Cells Method is superfluous




with noted exceptions.

Set MyRange = Range("A1:A10").Columns
? Range(MyRange(1,2),MyRange(1,7)).address '<== Raises an error
? MyRange.Range(MyRange.Cells(1, 2), MyRange.Cells(1, 7)).Address
$B$1:$G$1


Yes indeed! I should have said "once a range that is a collection of
cells is assigned to an object variable . . . ."

Thanks for pointing it out,
Alan Beban
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
Defining a Range using Variables Billyruben Excel Discussion (Misc queries) 3 December 2nd 08 06:31 PM
Sum a range based on two variables billy Excel Worksheet Functions 2 May 15th 08 07:03 PM
How do I select on two variables in a range of data in excel Jeff Excel Worksheet Functions 7 September 13th 05 01:10 AM
Add up a Dynamic Range with 2 Variables John Excel Worksheet Functions 1 January 15th 05 02:23 PM
Range Variables Daniel[_7_] Excel Programming 1 November 20th 03 05:45 AM


All times are GMT +1. The time now is 02:30 PM.

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

About Us

"It's about Microsoft Excel"