Copy Visible Cells Only

dmbpt

New Member
Joined
Nov 22, 2012
Messages
7
Hi guys,

I have a simple operation which is driving me nuts! I have the following code (below) to copy my active worksheet and paste it at the end of my excel file in order to keep an archive of the report.

The code works, however, this is an 8000 row report, with an auto filter - and when i run the code, the hidden rows are copied as well. ie: i have 300 visible rows, however the copy gives me all 8000. i'd like to keep all the formatting / objects as well.

thanks in advance!

Sub CopySheet()
Dim sheetname As String
sheetname = UCase(Left(Sheet18.Cells(11, 17), 3)) & Sheet18.Cells(13, 17)
For Each ws In ThisWorkbook.Worksheets
If InStr(ws.Name, sheetname) > 0 Then
chk = True
cnt = cnt + 1
End If
Next
If chk Then
sheetname = sheetname & " (" & cnt & ")"
End If
Worksheets("REPORT").Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = sheetname
ActiveSheet.UsedRange.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Code:
Activesheet.UsedRange.SpecialCells(xlCellTypeVisible).Select
 
Upvote 0
Hi Kenneth,

This doesn't solve the problem. I still have hidden rows in the middle :(

I don't know what's happening
 
Upvote 0
Try (totally untested)...

Code:
Sub CopySheet()
    Dim sheetname As String
    sheetname = UCase(Left(Sheet18.Cells(11, 17), 3)) & Sheet18.Cells(13, 17)
    For Each ws In ThisWorkbook.Worksheets
        If InStr(ws.Name, sheetname) > 0 Then
            chk = True
            cnt = cnt + 1
        End If
    Next
    
    If chk Then sheetname = sheetname & " (" & cnt & ")"

    With ThisWorkbook
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = sheetname
    End With
    
    Worksheets("REPORT").UsedRange.SpecialCells(12).Copy
    Sheets(sheetname).Cells(1, 1).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,247
Messages
6,123,853
Members
449,129
Latest member
krishnamadison

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top