PC Review


Reply
Thread Tools Rate Thread

Copy Range and Assign a Defined Name to the Pasted Range

 
 
sgltaylor
Guest
Posts: n/a
 
      5th Dec 2009
Hi All,

I need some help with the following:

I am looking for the code which will allow me to copy a range
from one worksheet to another and at the same time assign
a defined name to the pasted area. For example, the range
A1 to A30 on worksheet 2 is copied and pasted to worksheet 1
starting in cell B30. A defined name of "result" is also created
at the same time.

The trick is the worksheets will not always be the same and
neither will the pasted ranges or the address where the
range is to be pasted.

Any help would be greatly appreciated.

Thanks,

Steve
 
Reply With Quote
 
 
 
 
Mike H
Guest
Posts: n/a
 
      5th Dec 2009
Hi,

Try this

Sub sonic()
Dim SrcRange As Range
Dim DestRange As Range
Set SrcRange = Application.InputBox(prompt:="Select cells to copy", Type:=8)
Set DestRange = Application.InputBox(prompt:="Select top left cell of
destination range", Type:=8)
If Not SrcRange Is Nothing And Not DestRange Is Nothing Then
SrcRange.Copy Destination:=DestRange
Else
MsgBox "You must select the Source and destination ranges"
End If
End Sub

Mike

"sgltaylor" wrote:

> Hi All,
>
> I need some help with the following:
>
> I am looking for the code which will allow me to copy a range
> from one worksheet to another and at the same time assign
> a defined name to the pasted area. For example, the range
> A1 to A30 on worksheet 2 is copied and pasted to worksheet 1
> starting in cell B30. A defined name of "result" is also created
> at the same time.
>
> The trick is the worksheets will not always be the same and
> neither will the pasted ranges or the address where the
> range is to be pasted.
>
> Any help would be greatly appreciated.
>
> Thanks,
>
> Steve
> .
>

 
Reply With Quote
 
marcus
Guest
Posts: n/a
 
      5th Dec 2009
Hi Steve
T
he worksheet not always being the same is no problem you just run the
code from the sheet your on, the start sheet. The destination sheet
for your paste must have some sort of rule, there must be some way to
identify which sheet you are about to paste to.

For example you may have a unique word on the destination sheet that
you can look for prior to pasting. Can you give some more information
on this please. There should also be some sort of rule for the range
size to copy. Is it the used range on the start sheet? Does it
always start in a given point like A1. Anyways if you could provide
more information you will get a better answer. In the mean time here
is an answer with some smarts built in.

Ask any questions you wish if it does not help.

Take care

Marcus

'Run this from the sheet you wish to copy from.

Option Explicit
Sub MoreInfoPls()
Dim sh As Worksheet
Dim ws As Worksheet
Dim lw As Integer
Dim lwb As Integer
Dim lwc As Integer

Set sh = ActiveSheet
Set ws = Sheets("Sheet1")
lw = Range("A" & Rows.Count).End(xlUp).Row
lwb = ws.Range("B" & Rows.Count).End(xlUp).Row
Range("A1:B" & lw).Copy 'Copies to the last used row in col B
ws.Range("B" & lwb).PasteSpecial xlValues 'Pastes just the vals
lwc = ws.Range("C" & Rows.Count).End(xlUp).Row 'Last used row in C
ws.Range("B" & lwb & ":C" & lwc).Name = "Result"

End Sub
 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      5th Dec 2009
I forgot about naming the range

Sub sonic()
Dim MySelection As Range, SrcRange As Range
Dim DestRange As Range
Set SrcRange = Application.InputBox(prompt:="Select cells to copy", Type:=8)
Set DestRange = Application.InputBox(prompt:="Select top left cell of
destination range", Type:=8)
If Not SrcRange Is Nothing And Not DestRange Is Nothing Then
SrcRange.Copy Destination:=DestRange
ActiveWorkbook.Names.Add Name:="result", _
RefersToR1C1:=DestRange.Resize(SrcRange.Rows.Count, SrcRange.Columns.Count)
Else
MsgBox "You must select the Source and destioantion ranges"
End If
End Sub

Mike

"Mike H" wrote:

> Hi,
>
> Try this
>
> Sub sonic()
> Dim SrcRange As Range
> Dim DestRange As Range
> Set SrcRange = Application.InputBox(prompt:="Select cells to copy", Type:=8)
> Set DestRange = Application.InputBox(prompt:="Select top left cell of
> destination range", Type:=8)
> If Not SrcRange Is Nothing And Not DestRange Is Nothing Then
> SrcRange.Copy Destination:=DestRange
> Else
> MsgBox "You must select the Source and destination ranges"
> End If
> End Sub
>
> Mike
>
> "sgltaylor" wrote:
>
> > Hi All,
> >
> > I need some help with the following:
> >
> > I am looking for the code which will allow me to copy a range
> > from one worksheet to another and at the same time assign
> > a defined name to the pasted area. For example, the range
> > A1 to A30 on worksheet 2 is copied and pasted to worksheet 1
> > starting in cell B30. A defined name of "result" is also created
> > at the same time.
> >
> > The trick is the worksheets will not always be the same and
> > neither will the pasted ranges or the address where the
> > range is to be pasted.
> >
> > Any help would be greatly appreciated.
> >
> > Thanks,
> >
> > Steve
> > .
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy formulas in defined range RE: VLOOKUP fORMULA Microsoft Excel Programming 4 15th Jun 2009 09:46 AM
Re: Copy formulas in defined range Per Jessen Microsoft Excel Programming 0 15th Jun 2009 09:10 AM
Copy pasted name range pgarcia Microsoft Excel Programming 5 18th Feb 2008 06:37 PM
Name defined Range - Copy? =?Utf-8?B?Sm9obiBCdWNrbGV5?= Microsoft Excel Misc 1 1st Aug 2006 02:36 AM
Copy-Paste Value based on user defined range tushargarg729@yahoo.com Microsoft Excel Discussion 0 24th Feb 2005 07:16 PM


Features
 

Advertising
 

Newsgroups
 


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