簡體   English   中英

使用 AUTOFILTER 將過濾后的數據復制到沒有標題行的列表底部(不同的 Excel 工作表)

[英]Using AUTOFILTER to copy filtered data to the bottom of a list (different Excel Sheet) without Header Row

我正在使用 AUTOFILTER 過濾一個 Excel 工作表 (Input_Wkr_Hrs) 上的數據,並將數據附加到同一工作簿中另一個工作表(輸出)上的列表(表)的底部。 我遇到的問題是,當數據添加到列表底部時,它包含 HEADER ROW 名稱。 如何使用 AUTOFILTER 復制過濾后的數據,並且在附加到列表底部時不包含 HEADER NAMES?

工作表上的源表 (Input_Wkr_Hrs) 包含三列(Emp Name、Section、Hours)。

我正處於學習 Excel VBA 的初級階段; 這是我關於 Stackoverflow 的第一個問題。 下面的代碼

Sub GetWorkerData() 'shImput_Wkr_Hrs name if source data sheet' shOutout name of destination sheet Dim rg As Range Set rg = shImput_Wkr_Hrs.Range("C15").CurrentRegion' 'TurnOffFunctionality ' 關閉屏幕更新和其他東西

With rg
    .AutoFilter field:=3, Criteria1:=">0"    ' filter working hrs hours >0
    .SpecialCells(xlCellTypeVisible).Copy    'Destination:=shOutput.Range("a1")
    '
    '.Offset(1).Resize(Selection.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy
    shOutput.Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlPasteValues

' rg.AutoFilter End With ' shOutput.Activate 'TurnOnFunctionality ' 打開屏幕更新和其他東西 End Sub

假設 ws_Data 包含您要過濾/復制的數據,而 ws_Output 是將粘貼數據的工作表。

Dim rng_Visible as Range, outputLastRow as long
' Assume that the first line is the header
With ws_Data
    .Range("A1").AutoFilter Field:=3, Criteria1:=">0"

    ' Check if there is data to copy
    If .AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1 > 0 Then
        Set rng_Visible = .AutoFilter.Range.Offset(1, 0).Resize(.AutoFilter.Range.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        ' Copy the visible filtered range
        rng_Visible.Copy

        ' Find the last row in Output worksheet using column A, make sure the column you use does not have any blank value in middle
         outputLastRow= ws_Output.Cells(ws_Output.Rows.Count, "A").End(xlUp).Row

         ' Paste to the output worksheet starting from column A
         ws_Output.Range("A" & outputLastRow).PasteSpecial xlPasteAll
         Application.CutCopyMode = False

         ' Turn off the auto filter 
         ws_Data.AutoFilterMode = False
    End If

End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM