Consulting

Results 1 to 8 of 8

Thread: PasteSpecial to another workbook

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    PasteSpecial to another workbook

    This works
    [vba]
    Sub Macro1()
    Dim Rng1 As Range, Rng2 As Range
    Set Rng1 = ActiveSheet.Range("E5").Resize(10)
    Set Rng2 = Workbooks("Book1.xls").Sheets(1).Range("A1")
    Rng1.Copy
    Rng2.PasteSpecial Paste:=xlValues, Transpose:=True
    End Sub

    [/vba]
    but not this
    [vba]
    Sub GetData()
    Dim Tgt As Range, i As Long
    Set Tgt = Workbooks("distribution_factors.xls").Sheets("Dist. Factors").Range("E11")
    For i = 5 To Range("B5").End(xlDown).Row
    Cells(i, 2).Resize(, 8).Copy
    Tgt.PasteSpecial Paste:=xlValues, Transpose:=True
    Next

    End Sub

    [/vba]
    Runtime error 9 - Subscript out of range.
    Any ideas?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Similar code works for me Malcolm. It must really be a wrong name.

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks for that.
    I can the pastespecial working in a trial workbook, but not using the OP's original. http://vbaexpress.com/forum/showthread.php?t=9527
    It's a minor niggle only, activating the workbook, but it doesn't seem to like it otherwise.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Surely, it is because that worksheet is protected?

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I've tried protecting the sample sheet and workbook. Here's my test workbooks
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    mdmackillop

    I've downloaded the attachment and I can't reproduce the error you mention.

    Both subs error with a message saying that cell you are trying to change is protected.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If I remove the sheet protection, it works fine.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Norie,
    The code has to be run from the Sample Distro workbook.

    Bob,
    As you'll have seen from the original post, removing protection is not really an option.

    Thanks for the input, the workaround is straightforward, so I guess can live with it.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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