PDA

View Full Version : Copy fail in Automation



lclqt12
01-22-2007, 04:30 PM
I use code :

Dim wb as Variant
set wb = Excel.WorkBooks.Open(path)
Windows(FileName).Activate 'FileName is the name of excel file which is
'opened by using Excel.WorkBooks.Open and it's path is path variable

Sheets(1).Select
Range("A1:B3").Select
Selection.Copy

When that code is run, it alert "Copy fail in Automation ..... ".

How could i copy content of a file in Automation?

lucas
01-22-2007, 04:36 PM
Are you running this from Excel?

lucas
01-22-2007, 05:01 PM
It is different if your trying to copy from Excel to Excel but I don't think that's what your doing or it wouldn't be posted in Automation, right?

Maybe something like this but we would need more information from you go guess any further....

Dim objExcel As New Excel.Application
Dim wb As Excel.Workbook
Dim FName As String
Dim Tmp

FName = "f:\AAA\Data1.xls"
Set wb = objExcel.Workbooks.Open(FName)

wb.Sheets(1).Range("A1:B3").copy

lclqt12
01-22-2007, 06:55 PM
I have 1 excel file Excel1.xls. And i write macro in it. And i have another excel file name Excel2.xls. I open Excel1.xls and write following code :

Dim wb as Variant
set wb = Excel.WorkBooks.Open(path) ' path is pointed to Excel2.xls
Windows("Excel2.xls").Activate
Sheets(1).Select
Range("A1:B1").Select
Selection.Copy

When i run that code, it alerts "Copy fail in Automation ..... ".

geekgirlau
01-22-2007, 07:28 PM
If you are running this from Excel, you can simplify Lucas' code even further:




Dim wb As Workbook
Dim FName As String


FName = "f:\AAA\Excel2.xls"
Set wb = Workbooks.Open(FName)

wb.Sheets(1).Range("A1:B3").copy

lucas
01-22-2007, 07:46 PM
Hello lclqt12,
Your problem is easy to solve but I think we are having a language problem. Can you tell us what language is yours and maybe we can find someone to make this easier.

shades
01-22-2007, 07:50 PM
Is this Mac or Win?

lucas
01-22-2007, 07:58 PM
This will copy the range A1:A3 to sheet Final Results from workbook Social Club.xls sheet RESULTS range A1:A3

Option Explicit
Sub GetDataFromClosedWorkbook()
Dim wb As Workbook
Application.ScreenUpdating = False ' turn off the screen updating

'Make path selections below
' Set wb = Workbooks.Open("f:\Temp\Social Club.xls", True, True)
Set wb = Workbooks.Open(ActiveWorkbook.Path & "\Social Club.xls")
' the sheet in this workbook to copy to
With ThisWorkbook.Worksheets("Final Results")
' read data from the source workbook
'the range to copy to in this workbook-name of sheet to copy FROM-range in closed workbook to copy
.Range("A1", "B3").Formula = wb.Worksheets("RESULTS").Range("A1", "B3").Formula
' .Range("R7", "U36").Formula = wb.Worksheets("RESULTS").Range("R7", "U36").Formula

End With
wb.Close False ' close the source workbook without saving any changes
Set wb = Nothing ' free memory
Application.ScreenUpdating = True ' turn on the screen updating
End Sub

lucas
01-22-2007, 07:59 PM
Good Question Rich.....

lucas
01-22-2007, 09:18 PM
Should this be moved to the Mac forum...?
This thread implies that it is for mac Rich.
http://vbaexpress.com/forum/showthread.php?t=11136