Results 1 to 7 of 7

Thread: VB for Excel:"paste"

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Argentina, Buenos Aires
    Posts
    38

    VB for Excel:"paste"

    I am using VB for Excel. I want to copy from a file and paste in another file.
    I open the first file and write

    Sheet.application.worksheets("Sheet1").range("A1:E1").select
    Sheet.application.worksheets("Sheet1").range("A1:E1").copy

    Then I open the second file (closing or not the first file does not make a difference) and write

    Sheet.application.worksheets("Sheet1").range("A1:E1").select
    Sheet.application.worksheets("Sheet1").range("A1:E1").paste

    It gives me ERROR at the last line, it does not accept "paste". I tried all possible changes. Any help?

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: VB for Excel:"paste"

    I don't recall exact syntax, however Excel has Selection object (it belongs to an Application object in the hierarchy if I am not mistaken) which in turn has the Paste method.
    So, once you select your Range then use Selection.Paste instead of what you did.

  3. #3

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: VB for Excel:"paste"

    .Paste is a method of worksheet (and few other types of objects), it is not a method of Range so why you get the error.

    To copy a range you don't need to select that range prior to copy.

    Try this:
    Code:
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
                
        '-- assumed both workbooks already opened
        Set ws1 = Workbooks("BookName1.xls").Worksheets("Sheet1")
        '-- or if yourcode is in the Active workbook:
        ' Set ws1 = ThisWorkbook.Worksheets("Sheet1")
        Set ws2 = Workbooks("BookName2.xls").Worksheets("Sheet1")
        
        '-- only need specify the first cell of destination
        ws1.Range("A1:E1").Copy Destination:=ws2.Range("A1")
    The last line above is the simplest way, however it can be replaced by:
    Code:
        ws1.Range("A1:E1").Copy
        ws2.Paste Destination:=ws2.Range("A1")
    or as below (not recommended):
    Code:
        Workbooks("BookName2.xls").Activate
        ws2.Activate
        ws2.Range("A1").Select
        ws2.Paste
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Argentina, Buenos Aires
    Posts
    38

    Re: VB for Excel:"paste"

    Un fortunatelly, it doesn't work!!

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Argentina, Buenos Aires
    Posts
    38

    Re: VB for Excel:"paste"

    Unfortunatelly, as I put the code

    Dim ws1 as worksheet

    It gives " compilation error, it has not been defined the type defined by the user" ( I am translating the message from spanish)

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB for Excel:"paste"

    Quote Originally Posted by Norah
    It gives " compilation error, it has not been defined the type defined by the user" ( I am translating the message from spanish)
    Where are you running this from?

Posting Permissions

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



Click Here to Expand Forum to Full Width