+ Reply to Thread
Results 1 to 9 of 9

Copy PasteSpecial without hidden lines

  1. #1
    Registered User
    Join Date
    01-28-2011
    Location
    Minnesota, USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Copy PasteSpecial without hidden lines

    I have done a fair amount of searching about this and have yet to find a working solution...

    Goal – to programmatically copy spreadsheets (in Excel 2007) from one workbook and paste them into a new one. These sheets contain hidden rows which should not be in the new one.

    Problem - The hidden rows are showing in the new one.

    I am working with existing code - originally in Excel 2003.

    Please Login or Register  to view this content.
    ___________________________________________
    Attempted fix 1
    Please Login or Register  to view this content.
    The "Selection.Copy" line produces this error:
    "Run-time error '1004':
    This command cannot be used on multiple selections."
    __________________________________________________
    Attempted fix 2
    Please Login or Register  to view this content.
    The "Selection.PasteSpecial ..." line produces this error:
    "Run-time error '1004':
    Application-defined or Object-defined error"
    Note: I tried several versions of the above one including copying it as a picture.
    ___________________________________________

    I am fairly new to VBA coding so any help would be greatly appreciated.

    Bill

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Copy PasteSpecial without hidden lines

    Hi Bill,

    This quick test worked fine for me:

    Please Login or Register  to view this content.
    Last edited by Colin Legg; 02-16-2011 at 10:02 AM.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    01-28-2011
    Location
    Minnesota, USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copy PasteSpecial without hidden lines

    Colin - Thanks for your reply, I think it is getting me closer.

    I didn't include all the code as it is lengthy but this workbook includes a number of sheets and loops through them.
    I applied your suggestion to this and am now getting the sheets created however they are blank.

    Here is the code (almost all of it - tried to strip out things that weren't pertinent) - you will see the stuff I added as "new code"...
    NOTE: these sheets are protected - when I tried to unprotect them in the current workbook prior to "Set rngCopy = Sheets(n).UsedRange.SpecialCells(xlCellTypeVisible)" I get error "PasteSpecial method on Range class failed" on "rngTarget.PasteSpecial xlPasteValues"

    Please Login or Register  to view this content.

    I also tried using it closer to yours by not activating sheets before copy and pasting:
    New Code:
    Please Login or Register  to view this content.
    Last edited by wb4syth; 02-16-2011 at 11:11 AM.

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Copy PasteSpecial without hidden lines

    I've changed as little of your code as possible, but hopefully enough so that it 'works'. I've tried to remove most/all of your Select / Activate and used Workbook/Worksheet/Range variables to reference the correct objects.
    Please Login or Register  to view this content.
    Last edited by Colin Legg; 02-16-2011 at 12:49 PM.

  5. #5
    Registered User
    Join Date
    01-28-2011
    Location
    Minnesota, USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copy PasteSpecial without hidden lines

    Okay - so now I am wondering if there is some issue with my Excel 2007.
    I created a new test workbook and reduced the code to its basics and still it pastes nothing in.

    See attachment of the test sheet

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by wb4syth; 02-16-2011 at 12:47 PM. Reason: added note about attachment

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Copy PasteSpecial without hidden lines

    Try changing this line:
    Please Login or Register  to view this content.
    To this:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    01-28-2011
    Location
    Minnesota, USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copy PasteSpecial without hidden lines

    Colin - you've been a great help...

    That worked for my test case.
    But - I applied it to the original and the result is only one sheet showed up (and on every new one created the same data was pasted).

    So the one that showed up was the only one that was not protected.

    So I added this first line before setting the source:
    Please Login or Register  to view this content.
    And on the "rngSource.Copy" line I get "Cannot change part of a merged cell"...

  8. #8
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Copy PasteSpecial without hidden lines

    I would recommend that you unmerge all merged cells.

    Catering for merged cells makes VBA more complicated than it needs to be. Formatting the cells to align by Centering Across Selection is the usual way to get the same visual effect as merged cells.

  9. #9
    Registered User
    Join Date
    01-28-2011
    Location
    Minnesota, USA
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Copy PasteSpecial without hidden lines

    Well that works
    Thanks for all your help and quick responses!

    Of course this workbook has tons of formatting and liberal use of merged cells...so I have my work cut out for me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1