PC Review


Reply
Thread Tools Rate Thread

Copy contents to a selected sheet in another workbook

 
 
Razorboy
Guest
Posts: n/a
 
      4th Nov 2008
Hello everyone,

i have created a macro that will copy and paste values and formats
from one sheet into "Sheet1" in another workbook. However i need to
copy the data into the sheet of the same name. Is this possible? The
sheet name in the orininal is currently variable based on a list and
is linked to cell N1. How do i modify the macro below to acheive
this?

Sub Test()
Dim bk As Workbook
Dim bSave As Boolean
Dim lRow As Long

On Error Resume Next
Set bk = Workbooks("Flat Files.xls")
On Error GoTo 0
If bk Is Nothing Then
bSave = True
Set bk = Workbooks.Open("C\Flat Files.xls")
End If

lRow = bk.Worksheets("Sheet1").Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
ThisWorkbook.ActiveSheet.Range("CopyRange").Copy
bk.Worksheets("Sheet1").Cells(lRow, 1).PasteSpecial xlPasteValues
bk.Worksheets("Sheet1").Cells(lRow, 1).PasteSpecial xlPasteFormats
bk.Worksheets("Sheet1").Cells(lRow, 1).PasteSpecial
xlPasteColumnWidths

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      4th Nov 2008
Dim myName as string
myName = someworkbook.worksheets("somesheetname").range("N1").value

Then you could use:
bk.Worksheets(myName).Cells(lRow, 1).PasteSpecial xlPasteValues

(If I understood the question.)


Razorboy wrote:
>
> Hello everyone,
>
> i have created a macro that will copy and paste values and formats
> from one sheet into "Sheet1" in another workbook. However i need to
> copy the data into the sheet of the same name. Is this possible? The
> sheet name in the orininal is currently variable based on a list and
> is linked to cell N1. How do i modify the macro below to acheive
> this?
>
> Sub Test()
> Dim bk As Workbook
> Dim bSave As Boolean
> Dim lRow As Long
>
> On Error Resume Next
> Set bk = Workbooks("Flat Files.xls")
> On Error GoTo 0
> If bk Is Nothing Then
> bSave = True
> Set bk = Workbooks.Open("C\Flat Files.xls")
> End If
>
> lRow = bk.Worksheets("Sheet1").Cells(Rows.Count, 1) _
> .End(xlUp).Offset(1, 0).Row
> ThisWorkbook.ActiveSheet.Range("CopyRange").Copy
> bk.Worksheets("Sheet1").Cells(lRow, 1).PasteSpecial xlPasteValues
> bk.Worksheets("Sheet1").Cells(lRow, 1).PasteSpecial xlPasteFormats
> bk.Worksheets("Sheet1").Cells(lRow, 1).PasteSpecial
> xlPasteColumnWidths


--

Dave Peterson
 
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
Non-VBA method to open a workbook to a selected sheet? Lostguy Microsoft Excel Discussion 3 23rd Jun 2008 11:57 PM
Copy Range to a New WorkBook + Name Sheet a cell Value + Name WorkBook another Celll Value Corey Microsoft Excel Programming 2 2nd Nov 2006 05:01 AM
Coping Contents of one Sheet into Another Workbook Egon Microsoft Excel Programming 2 1st Dec 2005 05:24 PM
print only selected cell contents without loosing spread sheet for =?Utf-8?B?QW5uYSBUb3ppZXI=?= Microsoft Excel Misc 1 16th Sep 2005 05:22 PM
Export selected Sheet in a new workbook with old workbook name! gr8guy Microsoft Excel Programming 5 11th Sep 2004 04:13 PM


Features
 

Advertising
 

Newsgroups
 


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