简体   繁体   English

根据单元格值删除范围内的行

[英]Delte rows in a range based on a cell value

I have a data on employees in the range Q6:V100.我有 Q6:V100 范围内的员工数据。 The data is not in table and the data consists of 6 columns(job title, duty, pay etc.) and based on the first column which is "Currently employed" I want to delete the row where the employee is not employed anymore (="").数据不在表中,数据由 6 列(职务、职责、工资等)组成,基于第一列“当前受雇”,我想删除该员工不再受雇的行(= "")。 The first column shows "O" if employed and ""(just a blank) if no .第一列显示“O”(如果使用)和“”(只是一个空白)如果没有。 This is the code I have tried.这是我试过的代码。 But it shows error message但它显示错误消息

"Autofilter method of range class failed" “范围类的自动过滤方法失败”

and I have no idea what is wrong... It will grateful if someone can help !我不知道出了什么问题......如果有人能帮忙,我将不胜感激!

Sub Delete_Rows_Based_On_Value()

Dim ws As Worksheet


  Set ws = ThisWorkbook.Worksheets("Trainings")
  ws.Activate

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

  '1. Apply Filter
  ws.Range("Q6:V100").AutoFilter Field:=1, Criteria1:=""

  '2. Delete Rows
  Application.DisplayAlerts = False
    ws.Range("Q7:V100").SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True

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

End Sub

I would advise a more robust system, with a notation in Column Q stating: 1 = Employed, 0 = Not employed我会建议一个更强大的系统,在 Q 列中有一个符号说明:1 = 受雇,0 = 未受雇

Sub Delete_Rows_Based_On_Value()

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Trainings")
    ws.Activate

Dim i As Integer
Dim n as Integer

    n = ws.Range("Q6").CurrentRegion.Rows.Count

    For i = n + 5 To 6 Step -1
        If ws.Cells(i, 17).Value = 0 Then
            ws.Rows(i).Delete
        End If
    Next i

If you do not adapt the new system, change the If-statement to match your current system:如果您不适应新系统,请更改 If 语句以匹配您当前的系统:

If ws.Cells(i, 17).Value = "" Then

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

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