簡體   English   中英

用於過濾特定列並從其他列中刪除數據的 Excel VBA 代碼

[英]Excel VBA code to filter specific columns and delete the data from other columns

我正在嘗試根據名稱“IBM_Software”從我的 Excel 工作表中的 H 列過濾數據,並刪除同一工作表 J 列中的層次結構數據。 下面是我使用的代碼,但它沒有按照我想要的方式工作,它也刪除了頂部的行,這很糟糕。

Sub Delete_Rows_Based_On_Value()
'Apply a filter to a Range and delete visible rows
'Source: https://www.excelcampus.com/vba/delete-rows-cell-values/


'''
Dim ws As Worksheet

  'Set reference to the sheet in the workbook.
  Set ws = ThisWorkbook.Worksheets("BDDASHBTEMPLATEFYTD21BILLING3")
  ws.Activate 'not required but allows user to view sheet if warning message appears

  'Clear any existing filters
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0

  '1. Apply Filter
  ws.Range("A:P").AutoFilter Field:=8, Criteria1:="IBM_Software"

  '2. Delete Rows
  Application.DisplayAlerts = False
   Sheets("BDDASHBTEMPLATEFYTD21BILLING3").AutoFilter.Range.Offset(1).Delete xlShiftUp
  Application.DisplayAlerts = True


  '3. Clear Filter
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0

End Sub

您的解決方案不起作用可能意味着您遇到了錯誤。 您也不太可能在工作表的每一行中都有數據。 如果您使用每一行,則不能抵消一行。 我會讓你來弄清楚如何找到最后一行(LR)。 此解決方案僅刪除過濾后的行,您的解決方案將刪除所有行。

ws.Range("A1:P" & LR).AutoFilter Field:=8, Criteria1:="IBM_Software"
ws.Range("A1.P" & LR).Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

暫無
暫無

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

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