Consulting

Results 1 to 10 of 10

Thread: Copy fail in Automation

  1. #1

    Copy fail in Automation

    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?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Are you running this from Excel?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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....
    [VBA]
    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
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    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 ..... ".

  5. #5
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    If you are running this from Excel, you can simplify Lucas' code even further:


    [VBA]
    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
    [/VBA]

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Mac Moderator VBAX Expert shades's Avatar
    Joined
    May 2004
    Location
    Kansas City, USA
    Posts
    638
    Location
    Is this Mac or Win?

    Software: LibreOffice 3.3 on Mac OS X 10.6.5
    (retired Excel 2003 user, 3.28.2008 )
    Humanware: Older than dirt
    --------------------
    old, slow, and confused
    but at least I'm inconsistent!

    Rich

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    This will copy the range A1:A3 to sheet Final Results from workbook Social Club.xls sheet RESULTS range A1:A3

    [VBA]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
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Good Question Rich.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •