简体   繁体   English

使用 vba 过滤和删除 excel 中的行 - 宏

[英]Filter and delete rows in excel using vba - macro

i have used the below script.我使用了以下脚本。 Which deletes the rows not present in the filter condition.这将删除过滤条件中不存在的行。 I have tried the below code but still not able to achieve it,我已经尝试了下面的代码,但仍然无法实现,

Dim a As String
a = ComboBox1.Value
End Sub

Private Sub CommandButton1_Click()

Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim wsfilter As Worksheet
Dim CopyLastRow As Integer
Dim DestlastRow As Integer
Dim ws As Worksheet, rng As Range, LstRw As Long

    '1. open the workbook to copy from
    Workbooks.Open "C:\dd\Imports PY Plan Forecast.xlsx"
     '2. Define each workbook
    Set wsCopy = ThisWorkbook.Sheets("Input Data")
    Set wsDest = Workbooks("Imports PY Plan Forecast.xlsx").Sheets("Source")
    Set wsfilter = ThisWorkbook.Sheets("HR - Source")
    Set wsfilter2 = ThisWorkbook.Sheets("HR - Main Page")
    '3. Define last row in source data
    CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
    
'Filter column A and column B based on value



wsDest.Range("A1:H" & CopyLastRow).AutoFilter Field:=3, Criteria1:=wsfilter2.Range("B3").Value
wsDest.Range("A1:H" & CopyLastRow).AutoFilter Field:=5, Criteria1:=wsCopy.Range("H2").Value & "*"
Application.DisplayAlerts = False
ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
Application.DisplayAlerts = True
ThisWorkbook.Worksheets("Input Data").Range("A2:H" & CopyLastRow).SpecialCells(xlCellTypeVisible).Copy wsDest.Range("A2")

    'ThisWorkbook.Worksheets("Input Data").Range("A2:F" & CopyLastRow).Copy wsDest.Range("A2")
'3.1. Clear Filter
  On Error Resume Next
    wsDest.ShowAllData
  On Error GoTo 0
 '4. close and save source file
    
    Workbooks("Imports PY Plan Forecast.xlsx").Close SaveChanges:=True
end sub

but it is deleting the other rows which are not existing in the filter condition.但它正在删除过滤条件中不存在的其他行。 Any inputs?有什么输入吗?

.Resize doesn't care about visibility. .Resize不关心可见性。 Assuming that you want to delete the rows that are visible under the filter conditions set, you could make a Range that is explicitly the visible cells and then delete those rows:假设您要删除在过滤条件集下可见的行,您可以创建一个明确为可见单元格的Range ,然后删除这些行:

...
wsDest.Range("A1:H" & CopyLastRow).AutoFilter Field:=3, Criteria1:=wsfilter2.Range("B3").Value
wsDest.Range("A1:H" & CopyLastRow).AutoFilter Field:=5, Criteria1:=wsCopy.Range("H2").Value & "*"

wsDest.Range("A2:A" & CopyLastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete

wsDest.ShowAllData
...

(note that the Range to delete excludes row 1, which I guess is headers) (请注意,要删除的Range不包括第 1 行,我猜这是标题)

Finally, i did few research and found the soultion.最后,我做了一些研究并找到了灵魂。


Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim wsfilter As Worksheet
Dim CopyLastRow As Integer
Dim DestlastRow As Integer
'Dim LastRow As Long

    '1. open the workbook to copy from
    Workbooks.Open "C:\Imports PY Plan Forecast.xlsx"
     '2. Define each workbook
    Set wsCopy = ThisWorkbook.Sheets("Input Data")
    Set wsDest = Workbooks("Imports PY Plan Forecast.xlsx").Sheets("Source")
    Set wsfilter = ThisWorkbook.Sheets("HR - Source")
    Set wsfilter2 = ThisWorkbook.Sheets("HR - Main Page")
    '3. Define last row in source data
    CopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
    
    Dim WS As Worksheet: Set WS = Workbooks("Imports PY Plan Forecast.xlsx").Sheets("Source")

'Filter column A and column B based on value

    Application.ScreenUpdating = False
    With WS
        .AutoFilterMode = False
       ' LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
        With wsDest.Range("A2:H" & CopyLastRow) '--Assuming your header is in Row 2 and records start at Row 3.
            .AutoFilter Field:=3, Criteria1:=wsfilter2.Range("B3").Value '--Field:=3 is Column C if data starts at A
            .AutoFilter Field:=5, Criteria1:=wsCopy.Range("H2").Value & "*"
            Application.DisplayAlerts = False
            WS.ListObjects("Data").DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
            Application.DisplayAlerts = True
            
        End With
        .AutoFilterMode = False
    End With
    On Error Resume Next
    wsDest.ShowAllData
    On Error GoTo 0```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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