You are here:

Excel/copy only visible cells/filtered rows and hidden columns

Advertisement


Question
QUESTION: Hi Tom,

I am trying to copy visible cells on my worksheet and paste results to a summary tab.  The rows are filtered, and I have columns hidden because there is data I do not want to copy there as well.  My macro works until I get to the point noted below and then I get an error that the “Information cannot be pasted because the Copy area and the paste area are not the same size and shape.”   

I’m completely confused because they were not the same size and shape previously either, but the data successfully pasted.
Where it fails I want to copy the unfiltered rows in the unhidden columns, which at that point are D, E, F, J, X and Y.

I appreciate any help you can give me!

Patti

Private Sub getTop5(myVariable)
dim r As Range
Dim sheetName As String
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManualWith Worksheets(myVariable)

sheetName = myVariable & " MailMerge"

.Columns("A:AA").Hidden = False
.AutoFilterMode = False
.Columns("A:C").Hidden = True  ' Keep hidden
.Columns("G:I").Hidden = True  ' Keep hidden
.Columns("M:AA").Hidden = True
       
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' always copy D
       'Debug.Print r.Address(external:=True)
       .Columns("K:K").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("a1")
       .AutoFilterMode = False
.Columns("M:AA").Hidden = False
       
'------------------------------------------

.Columns("K:L").Hidden = True
.Columns("O:AA").Hidden = True
       .AutoFilterMode = False
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' emp# is in D
       'Debug.Print r.Address(external:=True)
       .Columns("M:M").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("G1")
       .AutoFilterMode = False
.Columns("K:L").Hidden = True
.Columns("O:AA").Hidden = False
       
'------------------------------------------
.Columns("K:N").Hidden = True
.Columns("Q:AA").Hidden = True
       .AutoFilterMode = False
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' emp# is in D
       'Debug.Print r.Address(external:=True)
       .Columns("O:O").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("M1")
       .AutoFilterMode = False
.Columns("K:N").Hidden = True
.Columns("Q:AA").Hidden = False
'------------------------------------------
.Columns("K:T").Hidden = True
.Columns("X:AA").Hidden = True
       .AutoFilterMode = False
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' emp# is in D
       'Debug.Print r.Address(external:=True)
       .Columns("U:U").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("S1")
       .AutoFilterMode = False
.Columns("K:T").Hidden = True
.Columns("X:AA").Hidden = False
'------------------------------------------
.Columns("K:W").Hidden = True
.Columns("Z:AA").Hidden = True
       .AutoFilterMode = False
       Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("Z1") ' <<<<<< ERRORS OUT HERE…“Information cannot be pasted because the Copy area and the paste area are not the same size and shape.”   
    .AutoFilterMode = False
    .Columns("K:W").Hidden = True
    .Columns("Z:AA").Hidden = False
       
End With
Application.Calculation = xlCalculationAutomatic
End Sub

ANSWER: Pattie,

this worked for me:

Private Sub getTop5(myVariable)
Dim r As Range
Dim sheetName As String
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

With Worksheets(myVariable)

sheetName = myVariable & " MailMerge"

.Columns("A:AA").Hidden = False
.AutoFilterMode = False
.Columns("A:C").Hidden = True  ' Keep hidden
.Columns("G:I").Hidden = True  ' Keep hidden
.Columns("M:AA").Hidden = True
       
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' always copy D
       'Debug.Print r.Address(external:=True)
       .Columns("K:K").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("a1")
       .AutoFilterMode = False
.Columns("M:AA").Hidden = False

'------------------------------------------

.Columns("K:L").Hidden = True
.Columns("O:AA").Hidden = True
       .AutoFilterMode = False
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' emp# is in D
       'Debug.Print r.Address(external:=True)
       .Columns("M:M").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("G1")
       .AutoFilterMode = False
.Columns("K:L").Hidden = True
.Columns("O:AA").Hidden = False

'------------------------------------------
.Columns("K:N").Hidden = True
.Columns("Q:AA").Hidden = True
       .AutoFilterMode = False
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' emp# is in D
       'Debug.Print r.Address(external:=True)
       .Columns("O:O").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("M1")
       .AutoFilterMode = False
.Columns("K:N").Hidden = True
.Columns("Q:AA").Hidden = False
'------------------------------------------
.Columns("K:T").Hidden = True
.Columns("X:AA").Hidden = True
       .AutoFilterMode = False
       Set r = .Range(.Range("D1"), .Range("D" & Rows.Count).End(xlUp)) ' emp# is in D
       'Debug.Print r.Address(external:=True)
       .Columns("U:U").AutoFilter Field:=1, Criteria1:="5", Operator:=xlTop10Items
        Set r = r.SpecialCells(xlCellTypeVisible)
        r.EntireRow.Copy Destination:=Worksheets(sheetName).Range("S1")
       .AutoFilterMode = False
.Columns("K:T").Hidden = True
.Columns("X:AA").Hidden = False
'------------------------------------------
.Columns("K:W").Hidden = True
.Columns("Z:AA").Hidden = True
       .AutoFilterMode = False
       Set r1 = .Range("A1:AA1").SpecialCells(xlVisible)
       Set r = r.SpecialCells(xlCellTypeVisible).EntireRow
       Set r = Intersect(r.EntireRow, r1.EntireColumn)
        r.Copy Destination:=Worksheets(sheetName).Range("Z1") ' <<<<<< ERRORS OUT HERE…“Information cannot be pasted because the Copy area and the paste area are not the same size and shape.”
    .AutoFilterMode = False
    .Columns("K:W").Hidden = True
    .Columns("Z:AA").Hidden = False
       
End With
Application.Calculation = xlCalculationAutomatic
End Sub


--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Perfect - thank you!

Very happy to have a solution.  Only other question if you don't mind, is whether there is a difference between SpecialCells(xlVisible) and .SpecialCells(xlCellTypeVisible) or if they are interchangeable.

Answer
Patti,

demo'd from the immediate window:

? xlVisible
12
? xlCellTypeVisible
12


so they are equivalent.  the first version is the original constant definition probably around Excel 95 or earlier.  I believe they introduced the more descriptive constants around Excel 97 but the old constants are still available.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.