Consulting

Results 1 to 3 of 3

Thread: Solved: Copy from one workbook to another

  1. #1
    VBAX Regular
    Joined
    Dec 2006
    Posts
    6
    Location

    Solved: Copy from one workbook to another

    I have a workbook name Updates. It has a command button that has the following code attached to it.
          Private Sub CommandButton1_Click()
      
          Workbooks.Open Filename:="C:\Documents and Settings\Administrator\Desktop\Sizzler\Inventory\WalkRoutes.xlsx"
          Workbooks("walkroutes.xlsx").Activate
          Sheets("WalkRoute").Activate
          Range("A1:G205").Select
          Range("A1:G205").Copy
    There's more code but my problem lies in this part of the code.
    What I am trying to do is copy the range A1 to G 205 in the workbook named "Walkroutes".
    then paste it into the workbook named "Updates"

    When I try to run the code it fails with a Runtime Error 1004 Select Method of Range Class Failed.

    Can someone help me with the code to make it copy the cells in the workbook "Walkroutes" and paste them into the workbook "Updates" ?

    Thank you for reading this and any help you can offer.

  2. #2
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi theSizz,

    Here's the simplistic way:

    [VBA]
    Private Sub CommandButton1_Click()

    'Opens workbook
    Workbooks.Open Filename:="C:\Documents and Settings\Administrator\Desktop\Sizzler\Inventory\WalkRoutes.xlsx"
    '//Not needed. Workbook is active when opened
    'Workbooks("walkroutes.xlsx").Activate
    '//Not really needed either
    'Sheets("WalkRoute").Activate
    '//Replace sheet name and range address
    ActiveWorkbook.Sheets("Walkroute").Range("A1:G205").Copy Workbooks("Updates").Sheets("PutNameHere").Range("PutAddressHere")

    [/VBA]

    and here's a better way

    [VBA]
    Option Explicit

    Sub ImBetter()

    Dim WSdest As Range
    Dim WSsource As Worksheet

    'Create object with workbook 'Updates'
    Set WSdest = ActiveWorkbook.Sheets("P").Range("A1")

    'Opens workbook
    Workbooks.Open Filename:="C:\Documents and Settings\Administrator\Desktop\Sizzler\Inventory\WalkRoutes.xlsx"

    'Create object with worksheet 'walkroute'
    Set WSsource = ActiveWorkbook.Sheets("walkroute")

    'Copy with destination
    WSsource.Range("A1:G205").Copy WSdest

    'Destroy objects
    Set WSdest = Nothing
    Set WSsource = Nothing

    End Sub
    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  3. #3
    VBAX Regular
    Joined
    Dec 2006
    Posts
    6
    Location
    WOW !
    Great............thanks so much.
    They both work (naturally), I decided to use the sub ImBetter.
    Nice and clean and descriptive.

    Thank you so much !!!

Posting Permissions

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