Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Using a cell's contents as a reference in Macros

Hi -

I am new to both the group and any 'advanced' Excel Macros, and was
hoping someone more knowledgeable than myself could assist.

I KNOW there is a solution to the following problem but am having
trouble getting it just right.

I need to be able to copy the values in a range of cells to another
worksheet, but the destination range on that sheet is defined by a
cell, and the reference it contains will be different each time.

Bearing in mind my VBA knowledge is limited, how should I go about
doing this?

Any tips much appreciated.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Using a cell's contents as a reference in Macros

How are you specifying the destination in your cell - is it the address (in
text) of the destination range? If so, then you could do this (In this
example I am only pasting the values, and that the input range is A1:C5; cell
E1 contains the address to copy to; and the destination is on Sheet2):

Range("A1:C5").Copy
Sheets("Sheet2").Range(Range("E1").Value).PasteSpe cial xlPasteValues

So, for example, if Range("E1").Value is "B2:D6", that is where the data
gets pasted.
HTH!

"JakeyC" wrote:

Hi -

I am new to both the group and any 'advanced' Excel Macros, and was
hoping someone more knowledgeable than myself could assist.

I KNOW there is a solution to the following problem but am having
trouble getting it just right.

I need to be able to copy the values in a range of cells to another
worksheet, but the destination range on that sheet is defined by a
cell, and the reference it contains will be different each time.

Bearing in mind my VBA knowledge is limited, how should I go about
doing this?

Any tips much appreciated.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Using a cell's contents as a reference in Macros

Assign the cell value to a variable:

Dim drng As Range 'Destination
Dim srng As Range 'Source

Set drng = Sheet1.Range("B2").Value 'cell with destination reference
Set srng = Sheet1.Range("C3:P200") ' Range to copy values from
drng.Value = srng.Value

Note: this code assumes that the cell with destination reference also
includes a Sheet reference similar to 'Sheet2!D3'. If it does not, post back
with an exact exanple of what the reference value would be like.

Mike F

"JakeyC" wrote in message
oups.com...
Hi -

I am new to both the group and any 'advanced' Excel Macros, and was
hoping someone more knowledgeable than myself could assist.

I KNOW there is a solution to the following problem but am having
trouble getting it just right.

I need to be able to copy the values in a range of cells to another
worksheet, but the destination range on that sheet is defined by a
cell, and the reference it contains will be different each time.

Bearing in mind my VBA knowledge is limited, how should I go about
doing this?

Any tips much appreciated.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Using a cell's contents as a reference in Macros

Just some added info:
If E1 is on the destination sheet (as I read the problem statement) then the
modification would be:

Range("A1:C5").Copy
Sheets("Sheet2").Range(sheets("sheet2").Range("E1" ).Value).PasteSpecial
xlPasteValues

--
Regards,
Tom Ogilvy


"K Dales" wrote in message
...
How are you specifying the destination in your cell - is it the address

(in
text) of the destination range? If so, then you could do this (In this
example I am only pasting the values, and that the input range is A1:C5;

cell
E1 contains the address to copy to; and the destination is on Sheet2):

Range("A1:C5").Copy
Sheets("Sheet2").Range(Range("E1").Value).PasteSpe cial xlPasteValues

So, for example, if Range("E1").Value is "B2:D6", that is where the data
gets pasted.
HTH!

"JakeyC" wrote:

Hi -

I am new to both the group and any 'advanced' Excel Macros, and was
hoping someone more knowledgeable than myself could assist.

I KNOW there is a solution to the following problem but am having
trouble getting it just right.

I need to be able to copy the values in a range of cells to another
worksheet, but the destination range on that sheet is defined by a
cell, and the reference it contains will be different each time.

Bearing in mind my VBA knowledge is limited, how should I go about
doing this?

Any tips much appreciated.




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Using a cell's contents as a reference in Macros

Thanks for your help; the reference cell does not currently specify the
sheet, but can do so. In response to Tom Ogilvy's update; the reference
cell is not on the same sheet as the destination so should be
straightforward.

Currently, I get an error telling me that an object is required when I
try to run the code as it stands - do I need to add a line or
something?

Again,

Many thanks



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Using a cell's contents as a reference in Macros

Then K Dales has given you what you need.

Think Mike rushed his answer. I would see his as



Dim drng As Range 'Destination
Dim srng As Range 'Source

Set drng = Sheet1.Range("B2").Value 'cell with destination reference
Set srng = Sheet1.Range("C3:P200") ' Range to copy values from
Sheet2.Range(drng.Value) = srng.Value

if drng didn't contain the sheet (which you say it doesn't) and if you
wanted to change it to contain the sheet name as well as the range, you
would alter the code to:

Dim drng As Range 'Destination
Dim srng As Range 'Source

Set drng = Sheet1.Range("B2").Value 'cell with destination reference
Set srng = Sheet1.Range("C3:P200") ' Range to copy values from
Range(drng.Value) = srng.Value

but either of this will require that the range specified in B2 matches the
size and shape of srng. K Dales on the other hand, only needs to specify
the top left corner of the destination range.

--
Regards,
Tom Ogilvy




"JakeyC" wrote in message
ups.com...
Thanks for your help; the reference cell does not currently specify the
sheet, but can do so. In response to Tom Ogilvy's update; the reference
cell is not on the same sheet as the destination so should be
straightforward.

Currently, I get an error telling me that an object is required when I
try to run the code as it stands - do I need to add a line or
something?

Again,

Many thanks



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Using a cell's contents as a reference in Macros

This has given me the solution to what would otherwise have been a
frustrating headache!

Many, many thanks.

Perhaps one day I'll be able to give others Excel advice - until then
I'll keep asking.

Cheers,

Jake

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
Using cell's contents as an index to a row ...? Mac Excel Worksheet Functions 3 February 16th 09 12:59 AM
link to a cell's contents John Bundy Excel Worksheet Functions 1 November 30th 06 09:43 PM
link to a cell's contents bluezcruizer Excel Worksheet Functions 0 November 30th 06 03:55 PM
Rotate a box with a cell's contents Circe Excel Discussion (Misc queries) 6 February 3rd 05 11:45 PM
macro to use a cell's contents as it's name Stephen Simons Excel Programming 2 June 11th 04 08:49 AM


All times are GMT +1. The time now is 01:58 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"