Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy data from another workbook to any open workbook

Hi,

I'm using XL2007 on Vista. I want to put in Personal.xlsb a code which
will copy range of data from another workbook and paste it to
activecell in any open workbook. I wrote some code but there seems to
be an error when trying to paste values("Pastespecial methow of range
class failed").

Sub Load()

Dim wbA As Workbook
Dim cellA As Range


Set wbA = Workbooks.Open(Filename:="H:\My Documents\TESTS\CALL
LIST.xls")
Windows("CALL LIST.xls").Activate
Workbooks("CALL LIST.xls").Worksheets(1).Range("A2:A20").Copy
Windows("CALL LIST.xls").Close (False)
Application.ActiveWindow.ActiveCell.Select
ActiveCell.PasteSpecial (xlPasteValues)

End Sub

Can someone advise on the solution?

thanks and regards
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Copy data from another workbook to any open workbook

Sometimes opening or closing a workbook will clear the clipboard.

I'd change the order around and use something like:

Option Explicit
Sub Load()

Dim wkbkSource As Workbook
Dim DestCell As Range
Dim RngToCopy As Range
Dim wkbkSourceWasOpen As Boolean
Dim myPath As String
Dim myFileName As String

myPath = "H:\My Documents\TESTS\"
If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If

myFileName = "call list.xls"

'where the paste should be
Set DestCell = ActiveCell

Set wkbkSource = Nothing
On Error Resume Next
Set wkbkSource = Workbooks(myFileName)
On Error GoTo 0

wkbkSourceWasOpen = True
If wkbkSource Is Nothing Then
'not open, so open it
wkbkSourceWasOpen = False
On Error Resume Next
Set wkbkSource = Workbooks.Open(Filename:=myPath & myFileName, _
ReadOnly:=True)
On Error GoTo 0

If wkbkSource Is Nothing Then
MsgBox "Source workbook couldn't be found!"
Exit Sub
End If
End If

Set RngToCopy = wkbkSource.Worksheets(1).Range("A2:A20")

RngToCopy.Copy
DestCell.PasteSpecial Paste:=xlPasteValues

If wkbkSourceWasOpen Then
'leave it open, do nothing
Else
wkbkSource.Close savechanges:=False
End If

End Sub

Untested, but it did compile.

Coder1215 wrote:

Hi,

I'm using XL2007 on Vista. I want to put in Personal.xlsb a code which
will copy range of data from another workbook and paste it to
activecell in any open workbook. I wrote some code but there seems to
be an error when trying to paste values("Pastespecial methow of range
class failed").

Sub Load()

Dim wbA As Workbook
Dim cellA As Range

Set wbA = Workbooks.Open(Filename:="H:\My Documents\TESTS\CALL
LIST.xls")
Windows("CALL LIST.xls").Activate
Workbooks("CALL LIST.xls").Worksheets(1).Range("A2:A20").Copy
Windows("CALL LIST.xls").Close (False)
Application.ActiveWindow.ActiveCell.Select
ActiveCell.PasteSpecial (xlPasteValues)

End Sub

Can someone advise on the solution?

thanks and regards


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Copy data from another workbook to any open workbook

Hi Dave,

That works perfectly, thank you! The code is more sophisticated an you
even added parts to handle missing file and building up path +
filename string.

Thank you for your time and have a Happy New Year!

regards


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,480
Default Copy data from another workbook to any open workbook

Hi

You could do it without opening the source workbook.

here is some code from Ron de Bruin's website
http://www.rondebruin.nl/copy7.htm
which I have modified to suit your case.

Sub Load()
Application.ScreenUpdating = False
On Error Resume Next
'Call the macro GetRange
GetRange "H:\My Documents\TESTS", "CALL LIST.xls", "Sheet1", "A2:A20", _
ActiveCell
On Error GoTo 0
Application.ScreenUpdating = True
End Sub

Sub GetRange(FilePath As String, FileName As String, SheetName As String, _
SourceRange As String, DestRange As Range)
Dim Start
'Go to the destination range
Application.Goto DestRange
'Resize the DestRange to the same size as the SourceRange
Set DestRange = DestRange.Resize(Range(SourceRange).Rows.Count, _
Range(SourceRange).Columns.Count)
'Add formula links to the closed file
With DestRange
.FormulaArray = "='" & FilePath & "/[" & FileName & "]" & SheetName
_
& "'!" & SourceRange
'Wait
Start = Timer
Do While Timer < Start + 2
DoEvents
Loop

'Make values from the formulas
.Copy
.PasteSpecial xlPasteValues
.Cells(1).Select
Application.CutCopyMode = False
End With
End Sub

--
Regards
Roger Govier

"Coder1215" wrote in message
...
Hi,

I'm using XL2007 on Vista. I want to put in Personal.xlsb a code which
will copy range of data from another workbook and paste it to
activecell in any open workbook. I wrote some code but there seems to
be an error when trying to paste values("Pastespecial methow of range
class failed").

Sub Load()

Dim wbA As Workbook
Dim cellA As Range


Set wbA = Workbooks.Open(Filename:="H:\My Documents\TESTS\CALL
LIST.xls")
Windows("CALL LIST.xls").Activate
Workbooks("CALL LIST.xls").Worksheets(1).Range("A2:A20").Copy
Windows("CALL LIST.xls").Close (False)
Application.ActiveWindow.ActiveCell.Select
ActiveCell.PasteSpecial (xlPasteValues)

End Sub

Can someone advise on the solution?

thanks and regards


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
How to: Open closed workbook/Search data tables/Return data to open workbook Hugh Adams Excel Discussion (Misc queries) 0 August 18th 10 02:04 PM
Copy Data from Multiple Closed Workbooks to Open Workbook Kris Excel Programming 3 October 31st 06 08:36 PM
1.Open workbook. 2 copy data. 3 replace data. 4 save workbook. Cristobalitotom Excel Programming 0 July 6th 06 12:24 AM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Excel Programming 1 April 1st 06 08:48 PM
Open a password protected excel workbook from second workbook to fetch data using dynamic connection kaustav choudhury Excel Programming 2 April 3rd 04 06:18 AM


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