简体   繁体   中英

found 'TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy _ ' Destination:=Sheets(“Sheet8”).Range(“A1”)

I have a sheet named "Staffdb" with two named tables "PermTBL" and "StaffTBL", same headers "Girls" "ID" "Hire_date" and "Status". All of the current and historic staff are in PermTBL. I would like to filter PermTBL on the Status field for "A" meaning active and then copy these to the StaffTBL which is empty. After manually filtering the PermTBL with the Status down arrow and select only "A" I go in to test the code and get an apparent partial copy. My code is Option Explicit

Sub PermTBLtoStaffTBL()

Dim rgnsrc As Range Dim rgndest As Range

Set rgnsrc = Worksheets("Staffdb").Range("PermTBL")
Set rgndest = Worksheets("Staffdb").Range("StaffTBL")
rgnsrc.SpecialCells(xlCellTypeVisible).Copy rgndest

End Sub

Finally as an additional piece of information the StaffTBL appears to have hidden rows, 3-7 are not visible which appears to correspond with my missing data. I have tried to unhide to no avail. Suggestions as to where to go next? Must I loop through the table or have I made an error in my destination? New at this, and 3rd world internet speed, along with inability to have books delivered makes this a tedious process. Please bear with the NewBee.

New piece of information, I have found that if I unhide the entire sheet the correct data appears in the StaffTBL, of course the filter of the PermTBL also disappears, so apparently I was on the right track. Would still like comments and suggestions on programmatically (as opposed to manually) filtering PermTBL. I will continue to search sites for that, but any suggestions are appreciated.

Sub CopyData()

    Dim t As ListObject
    Dim t2 As ListObject

    Set t = ActiveSheet.ListObjects("PermTBL")
    Set t2 = ActiveSheet.ListObjects("StaffTBL")

    ' Remove all rows from StaffTBL table
    If Not t2.DataBodyRange Is Nothing Then
        t2.DataBodyRange.Rows.Delete
    End If

    ' Filter Status by "A"
    t.DataBodyRange.AutoFilter Field:=4, Criteria1:="A"
    ' Copy to first cell right below the table's header
    t.DataBodyRange.Copy t2.Range(1).Offset(1)
    ' Remove filter from PermTBL table
    t.DataBodyRange.AutoFilter

End Sub

UPDATE

Example workbook

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM