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

I keep getting Runtime error 9 - Subscript out of range
when my code hits the line:
rngCopy = Sheets("CTab").Range("CRange") in the code
below. The next line of code may generate the same error
because it contains similar syntax... it just hasn't made
it that far.
(I am populating variables with the worksheet and range
from one worksheet to copy the values held within that
range to a cell on another worksheet when the user clicks
on a command button. All is working fine except for the
persistent error message.) Using Excel 2000.

Dim rngCopy As Range
Dim rngPaste As Range
Dim CTab As Variant
Dim CRange As Variant
Dim PTab As Variant
Dim PRange As Variant
CTab = Sheets("Logic Questions").Range("A53").Value
CRange = Sheets("Logic Questions").Range("H53").Value
PTab = Sheets("Logic Questions").Range("A54").Value
PRange = Sheets("Logic Questions").Range("H54").Value
rngCopy = Sheets(CTab).Range(CRange)
rngPaste = Sheets(PTab).Range(PRange)
rngCopy.Copy
Sheets("Bill").Activate

Thank you in advance for any help you can provide,
Al

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Paste a Range from a variable

Al,

Since rngCopy is declared as a range, simple change

rngCopy = Sheets("CTab").Range("CRange")

to

Set rngCopy = Sheets("CTab").Range("CRange")

But, you could simple change

rngCopy = Sheets(CTab).Range(CRange)
rngPaste = Sheets(PTab).Range(PRange)

to

Sheets(CTab).Range(CRange).Copy Sheets(PTab).Range(PRange)

if you are copy/pasting rngeCopy to rngPaste.

HTH,
Bernie
MS Excel MVP

"Al" wrote in message
...
I keep getting Runtime error 9 - Subscript out of range
when my code hits the line:
rngCopy = Sheets("CTab").Range("CRange") in the code
below. The next line of code may generate the same error
because it contains similar syntax... it just hasn't made
it that far.
(I am populating variables with the worksheet and range
from one worksheet to copy the values held within that
range to a cell on another worksheet when the user clicks
on a command button. All is working fine except for the
persistent error message.) Using Excel 2000.

Dim rngCopy As Range
Dim rngPaste As Range
Dim CTab As Variant
Dim CRange As Variant
Dim PTab As Variant
Dim PRange As Variant
CTab = Sheets("Logic Questions").Range("A53").Value
CRange = Sheets("Logic Questions").Range("H53").Value
PTab = Sheets("Logic Questions").Range("A54").Value
PRange = Sheets("Logic Questions").Range("H54").Value
rngCopy = Sheets(CTab).Range(CRange)
rngPaste = Sheets(PTab).Range(PRange)
rngCopy.Copy
Sheets("Bill").Activate

Thank you in advance for any help you can provide,
Al



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default Paste a Range from a variable

I shouldn't have copied the code with the quotes from your message
text, but the code without the quotes from your copied code. But the
message is the same: you need to use "Set " when working with range
variables.

HTH,
Bernie
MS Excel MVP

"Bernie Deitrick" wrote in message
...
Al,

Since rngCopy is declared as a range, simple change

rngCopy = Sheets("CTab").Range("CRange")

to

Set rngCopy = Sheets("CTab").Range("CRange")

But, you could simple change

rngCopy = Sheets(CTab).Range(CRange)
rngPaste = Sheets(PTab).Range(PRange)

to

Sheets(CTab).Range(CRange).Copy Sheets(PTab).Range(PRange)

if you are copy/pasting rngeCopy to rngPaste.

HTH,
Bernie
MS Excel MVP

"Al" wrote in message
...
I keep getting Runtime error 9 - Subscript out of range
when my code hits the line:
rngCopy = Sheets("CTab").Range("CRange") in the code
below. The next line of code may generate the same error
because it contains similar syntax... it just hasn't made
it that far.
(I am populating variables with the worksheet and range
from one worksheet to copy the values held within that
range to a cell on another worksheet when the user clicks
on a command button. All is working fine except for the
persistent error message.) Using Excel 2000.

Dim rngCopy As Range
Dim rngPaste As Range
Dim CTab As Variant
Dim CRange As Variant
Dim PTab As Variant
Dim PRange As Variant
CTab = Sheets("Logic Questions").Range("A53").Value
CRange = Sheets("Logic Questions").Range("H53").Value
PTab = Sheets("Logic Questions").Range("A54").Value
PRange = Sheets("Logic Questions").Range("H54").Value
rngCopy = Sheets(CTab).Range(CRange)
rngPaste = Sheets(PTab).Range(PRange)
rngCopy.Copy
Sheets("Bill").Activate

Thank you in advance for any help you can provide,
Al





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 576
Default Paste a Range from a variable

Al,

Subscript Out-of-Range usually means that Excel doesn't recognize your
references. Or your references aren't there.

As CTab and CRange are variables - remove the quotes
rngCopy = Sheets("CTab").Range("CRange")
s/b
rngCopy = Sheets(CTab).Range(CRange)
(though there aren't quotes in the code)

Also - CTab and CRange (as used in the above code) should be Text.
Check these 2 lines and see what they come up with

CTab = Sheets("Logic Questions").Range("A53").Value
Msgbox CTab
CRange = Sheets("Logic Questions").Range("H53").Value
Msgbox CTab & " " CRange
should give you an indication

You may want to change the '.Value' to '.Text'

--
sb
"Al" wrote in message
...
I keep getting Runtime error 9 - Subscript out of range
when my code hits the line:
rngCopy = Sheets("CTab").Range("CRange") in the code
below. The next line of code may generate the same error
because it contains similar syntax... it just hasn't made
it that far.
(I am populating variables with the worksheet and range
from one worksheet to copy the values held within that
range to a cell on another worksheet when the user clicks
on a command button. All is working fine except for the
persistent error message.) Using Excel 2000.

Dim rngCopy As Range
Dim rngPaste As Range
Dim CTab As Variant
Dim CRange As Variant
Dim PTab As Variant
Dim PRange As Variant
CTab = Sheets("Logic Questions").Range("A53").Value
CRange = Sheets("Logic Questions").Range("H53").Value
PTab = Sheets("Logic Questions").Range("A54").Value
PRange = Sheets("Logic Questions").Range("H54").Value
rngCopy = Sheets(CTab).Range(CRange)
rngPaste = Sheets(PTab).Range(PRange)
rngCopy.Copy
Sheets("Bill").Activate

Thank you in advance for any help you can provide,
Al



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
copy range and paste into every 3rd cell of new range thomsonpa New Users to Excel 4 December 3rd 07 01:47 PM
How to copy&paste a variable range rows and colums IK Excel Discussion (Misc queries) 1 August 30th 06 12:06 AM
how do i use the function to copy and paste a determined variable. Strem Excel Worksheet Functions 2 January 8th 05 05:31 PM
Problem trying to us a range variable as an array variable TBA[_2_] Excel Programming 4 September 27th 03 02:56 PM
How to paste variable value onto a Cell? Milind Excel Programming 1 July 29th 03 09:55 PM


All times are GMT +1. The time now is 12:56 AM.

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"