+ Reply to Thread
Results 1 to 8 of 8

Problem when using PasteSpecial with SkipBlanks on filtered table

  1. #1
    Registered User
    Join Date
    05-31-2020
    Location
    Norfolk, England
    MS-Off Ver
    365
    Posts
    22

    Problem when using PasteSpecial with SkipBlanks on filtered table

    I have written a VBA script to produce a report (a subset of data) from an Excel table. Basically, the VBA script filters the table based on some user selected criteria. It then copies a range of cells (using SpecialCells(xlCellTypeVisible).Copy to ensure only the filtered data is copied). I am trying to then paste this into a new location. The new location already contains some data so I am using PasteSpecial (xlPasteValues), SkipBlanks:=True so that only cells containing data are pasted and blank cells are not.

    However, Excel is taking no notice of the "skipblanks" command, or perhaps it's just not recognising the blank cells as blank cells as it just pastes everything, blank cells included.

    I've tried the exact same thing using data copied normally (eg selection.copy) from the unfiltered table and it works perfectly, so the problem seems to be related to the fact that it is copied from a filtered table.

    Has anyone experienced this issue and, if so, is there a simple solution?

    TIA
    Mike

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    As SpecialCells method is useless in this case so try without it
    or just activating the Macro Recorder and operating manually …

  3. #3
    Registered User
    Join Date
    05-31-2020
    Location
    Norfolk, England
    MS-Off Ver
    365
    Posts
    22

    Re: Problem when using PasteSpecial with SkipBlanks on filtered table

    Thanks Marc,

    Unfortunately, if I don't use the SpecialCells method, Excel copies all data in the column, not just the filtered records. I've also tried recording a macro, but this doesn't achieve the required result either. I selected "skipblanks" when pasting the data, but it still pastes the blank cells over my existing data. This suggests to me that, when copying filtered data, Excel is not treating the blank cells as blanks, even though they definitely are.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Problem when using PasteSpecial with SkipBlanks on filtered table


    That's odd 'cause when operating manually with the Macro Recorder activated it wells works w/o SpecialCells
    as everyone must know the Copy method works only with visible cells ! (Many samples within this forum.)
    I guess you did not ever give it a try … Maybe what you see blank is not really blank …

  5. #5
    Registered User
    Join Date
    05-31-2020
    Location
    Norfolk, England
    MS-Off Ver
    365
    Posts
    22

    Re: Problem when using PasteSpecial with SkipBlanks on filtered table

    Thanks for your further comments Marc, yes I did try the manual macro recorder method, but it did not produce the correct results. As I mentioned, the code works perfectly if the table is not filtered, so the blank cells really are blank.

    I've attached a simple example. It copies 2 columns of data from a table to another area of the spreadsheet. It then copies the third column and pastes it over the top of the previously pasted data, but should only paste non-blank cells. Works fine if the table is not filtered but doesn't work properly if the table is filtered.

    The attached example uses three different methods (select special cells, select all cells and the recorded macro version). None work properly on a filtered table.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    From an Excel table the Copy method well works on selection only and thus on contiguous cells
    so it needs to select column A, Copy, Paste then the same for column C …

    To copy at once both columns SpecialCells method is usefull here
    so according to your attachment a VBA demonstration as a starter :

    PHP Code: 
    Sub Demo1()
             
    Application.ScreenUpdating False
             
    [A17].CurrentRegion.Clear
        With 
    [A1].CurrentRegion.Columns
            
    .Item(5).NumberFormat = .Cells(22).NumberFormat
            
    .Item(5).Formula "=IF(C1>0,C1,B1)"
             
    Union(.Item(1), .Item(5)).SpecialCells(xlCellTypeVisible).Copy
             
    [A17].PasteSpecial xlPasteValuesAndNumberFormats
            
    .Item(5).Clear
        End With
             Application
    .ScreenUpdating True
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  7. #7
    Registered User
    Join Date
    05-31-2020
    Location
    Norfolk, England
    MS-Off Ver
    365
    Posts
    22

    Re: Problem when using PasteSpecial with SkipBlanks on filtered table

    Thanks Marc, that works (and I've learnt a couple of new tips too ).

    I ended up doing something similar, in that I used an intermediate 'paste' step. I copied the filtered column, using CellTypeVisible and pasted it into some spare cells, then did a normal Copy of this and pasted it in the target range, using the SkipBlanks method and that worked perfectly. I still don't really understand why SkipBlanks doesn't work on the first paste, but guess it's just a quirk of VBA.

    Thank you for all your help.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    It seems the normal behavior is changed when using an Excel table …

    Thanks for the rep' !

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. .PasteSpecial problem
    By Akbarov in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-12-2020, 08:44 AM
  2. Problem with data order when looping through a filtered table with hidden columns
    By michielske in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-30-2018, 07:25 AM
  3. Using Copy/Destination with SkipBlanks
    By Ezzard in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2015, 12:58 PM
  4. [SOLVED] Problem when copy filtered table in Excel 2007 using VBA
    By josel2820 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-24-2015, 01:19 PM
  5. [SOLVED] pasteSpecial problem
    By BrownBoy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-23-2014, 06:02 PM
  6. PasteSpecial w/ SkipBlanks in VBA - Issue
    By Digital21000 in forum Excel General
    Replies: 0
    Last Post: 04-21-2011, 10:09 PM
  7. VBA PasteSpecial problem
    By achinfish in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-25-2007, 05:29 PM

Tags for this Thread

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