我有一个电子表格,该电子表格的列从A5到AA5,数据从A6到AA10000。在单元格A1中,用户输入一个值,在单元格A2中是一个包含X到AA列(A,B,C,D)标题的投递箱,而在A3中,我有一个逻辑运算符(<,> ,<>,=)。我正在尝试编写一个脚本,该脚本穿过X列到AA列,并删除满足用户设置的条件的单元格,例如用户输入值300,标题“ B”和逻辑运算符“ <”,宏将通过具有标题“ B”的列Y,并删除所有小于300的值,然后将行从A删除为机管局。
到目前为止,我已经尝试过:
Sub removedata()
Dim ws As Worksheet
Dim rng As Range
Dim headerval As Variant
Dim sign As Variant
Dim inputval As Variant
Dim b_header As Range
Dim Cell As Range
Set ws = Worksheets("Sheet1")
Set rng = ws.Range("X5:AA5000")
Set b_header = ws.Range("X5:X5000")
inputval = cells(1, 1).Value
headerval = cells(2, 1).Value
sign = cells(3, 1).Value
For Each Cell In b_header.cells
If (headerval = "B") And (sign = "<") And (inputval < Cell.Value) Then
Cell.Delete
End If
Next Cell
End Sub
我仅尝试将其用于B列,以测试是否可以进行某些操作。当我运行此宏时,它仅缓冲一秒钟,然后什么也没有发生。
任何帮助将不胜感激!
编辑:实际上,我意识到它会删除大于输入值(单元格A1)的值,但是每次运行它时它只会删除其中的一些值,还会将其下方的单元格移到其位置。
>答案 0 :(得分:0)
COUNTIF / COUNTIFS工作表函数接受并将标准解释为字符串。您可以使用Evaluate或直接通过应用程序对象使用。
Option Explicit
Sub delSpecial()
Dim lr As Long, i As Long, c As String, cl As Long
With Worksheets("sheet6")
c = .Cells(3, "A").Value & .Cells(1, "A").Value
cl = Application.Match(.Cells(2, "A").Value, .Rows(5), 0)
lr = Application.Max(.Cells(.Rows.Count, "X").End(xlUp).Row, _
.Cells(.Rows.Count, "Y").End(xlUp).Row, _
.Cells(.Rows.Count, "Z").End(xlUp).Row, _
.Cells(.Rows.Count, "AA").End(xlUp).Row)
For i = lr To 6 Step -1
If CBool(Application.CountIf(.Cells(i, cl), c)) Then
.Cells(i, "A").Resize(1, 27).Interior.Color = vbYellow
'.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub