简体   繁体   English

在Excel VBA中基于FALSE值删除行(运行时错误91)

[英]Delete rows based on FALSE value in Excel VBA (Runtime error 91)

I have a spreadsheet that needs to have rows deleted based off a "FALSE" value. 我有一个电子表格,需要根据“FALSE”值删除行。 Researching the net, I found some code from this site: 研究网络,我从这个网站找到了一些代码:

https://www.extendoffice.com/documents/excel/815-excel-remove-rows-based-on-cell-value.html https://www.extendoffice.com/documents/excel/815-excel-remove-rows-based-on-cell-value.html

Sub DeleteRows()

    Dim rng As Range
    Dim InputRng As Range
    Dim DeleteRng As Range
    Dim DeleteStr As String
    xTitleId = "Delete Based on Cell Value"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
DeleteStr = Application.InputBox("Delete Text", xTitleId, Type:=2)
For Each rng In InputRng
If rng.Value = DeleteStr Then
    If DeleteRng Is Nothing Then
        Set DeleteRng = rng
    Else
        Set DeleteRng = Application.Union(DeleteRng, rng)
    End If
End If
Next
DeleteRng.EntireRow.Delete '<----- *highlighted error*

End Sub

Upon execution of the macro, I get the "Runtime 91 object variable not set" error. 在执行宏时,我得到“运行时91对象变量未设置”错误。

I am aware that I need to set "DeleteRng" to an object, but I am not sure what. 我知道我需要将“DeleteRng”设置为一个对象,但我不确定是什么。 I am new to VBA and it could be something minor I am overlooking. 我是VBA的新手,可能是我忽视的一些小事。

You need to check first that DeleteRng contains something 您需要首先检查DeleteRng是否包含某些内容

Sub DeleteRows()

Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
Dim xTitleId As String

xTitleId = "Delete Based on Cell Value"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
DeleteStr = Application.InputBox("Delete Text", xTitleId, Type:=2)

For Each rng In InputRng
    If rng.Value = DeleteStr Then
        If DeleteRng Is Nothing Then
            Set DeleteRng = rng
        Else
            Set DeleteRng = Application.Union(DeleteRng, rng)
        End If
    End If
Next

If Not DeleteRng Is Nothing Then DeleteRng.EntireRow.Delete '<----- *highlighted error*

End Sub

The code works just fine for me when I test it, but I have a feeling I know what is happening when you are running it. 当我测试它时,代码对我来说效果很好,但我有一种感觉,我知道当你运行它时会发生什么。 Chances are, it isnt finding the text you are inputting into your box. 有可能,它找不到你输入到你的盒子里的文字。

Try putting a breakpoint at "DeleteRng.EntireRow.Delete". 尝试在“DeleteRng.EntireRow.Delete”处设置断点。 You can do this by clicking in the thin grey bar of the window to the left of your code. 您可以通过单击代码左侧窗口的细灰色栏来完成此操作。

I would also edit your code from: 我还会编辑你的代码:

DeleteRng.EntireRow.Delete '<----- *highlighted error*

to

If Not DeleteRng Is Nothing Then
    DeleteRng.EntireRow.Delete '<----- *highlighted error*
Else
    msgbox "No rows found!"
End If

This will provide the inverse of your object. 这将提供对象的反转。 If the object is set to something, then the inverse is nothing. 如果对象设置为某个东西,则反之则不然。 This will prevent your code from raising an error when an object isnt set. 这将防止您的代码在未设置对象时引发错误。 You can remove the else statement as well, but with the else statement in there it will tell you if nothing is set. 你也可以删除else语句,但是在那里使用else语句它会告诉你是否设置了什么。

(Posted on behalf of the OP) . (代表OP发布)

I figured out my mistake! 我想出了自己的错误! With a little help and modification to the code, as well as a suggestion, I realized that the value I was typing in was different from the returned value because it was case-sensitive. 通过对代码的一些帮助和修改,以及一个建议,我意识到我输入的值与返回值不同,因为它区分大小写。

Column B had the "FALSE" value displayed in all caps, but the immediate window showed the value was actually "False". B列在所有大写字母中都显示“FALSE”值,但是即时窗口显示的值实际上是“False”。 I searched correctly with "False" and it worked perfectly! 我用“False”正确搜索了它,它完美无缺!

I also changed the error part to: 我还将错误部分更改为:

If Not DeleteRng Is Nothing Then DeleteRng.EntireRow.Delete

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

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