excel - 查找有时会生成运行时错误 91

标签 excel vba

我有一个带有列标题的数据表。我有一个我不想要的列标题列表。

我想删除不需要的列标题,无论它们在工作表中的哪个位置,以及用户添加其他要删除的列的能力。

我明白了

run time 91 error



在这条线上:ws.Rows("1:1").Select.Find(T).EntireColumn.Delete
有时我会在代码的第一个循环中得到一个错误,有时它会在部分完成。

我看过其他帖子,但这些问题的相关性不足以让我解决我的问题。我尝试阅读一些关于定义对象的文章。我一直在使用 msgbox 命令来确保代码正在查找值,并且这似乎一直在工作,但它在 Find 处发生故障。命令。
Sub DeleteBadHeaders2()
    Dim FirstHeading As Range
    Set FirstHeading = Worksheets("Headings_To_Delete").Range("a2")
'Worksheet that has all the column headings I want deleted
    Dim x As Integer
'x is for the do while loop to individually highlight each cell
    Dim y As Long
    y = Worksheets("Headings_To_Delete").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
'y acts as the upper bound to the headings to delete column for the while loop
    Dim T As Variant
'T acts as a temporary value holder that will be used to delete the proper columns
    Dim ws As Worksheet
        Set ws = ActiveSheet
    x = 0
    Do While x < (y - 1)
        Worksheets("Headings_To_Delete").Range("a2").Offset(x, 0).Interior.Color = RGB(224, 0, 0)
'Calling the rage as above fixes the active cell problem
        Let T = Worksheets("Headings_To_Delete").Range("a2").Offset(x, 0).Value
        'MsgBox T & " is found."
        ws.Rows("1:1").Select.Find(T).EntireColumn.Select
        'for testing switch the last part of the code to EntireColumn.Interior.Color = RGB(0, 225, 0)
        x = x + 1
     Loop
'The loop is highlighting the cells incrementally based on the first active cell until the upper limit of how many cells are in the column
End Sub

最佳答案

ws.Rows("1:1").Select.Find(T).EntireColumn.Select

应该
ws.Rows(1).Find(T).EntireColumn.Select  'Delete?

通常,尽管无论何时使用 Find()最好通过测试 Nothing 的返回值来检查您是否真的找到了任何东西。在尝试做 Select 之类的任何事情之前或 Delete .

明确 Find 中的其他一些参数也是一个好主意,例如 lookAt例如。

像这样的东西:
Sub DeleteBadHeaders()

    Dim r As Long, lastRow As Long
    Dim T As Variant
    Dim ws As Worksheet, wsList As Worksheet, f As Range

    Set ws = ActiveSheet

    Set wsList = Worksheets("Headings_To_Delete")
    lastRow = wsList.Cells(Rows.Count, 1).End(xlUp).Row 'last row

    For r = 2 To lastRow
        T = wsList.Cells(r, "A").Value
        If Len(T) > 0 Then
            Set f = ws.Rows(1).Find(what:=T, lookat:=xlWhole)
            'check to see if the heading was found
            If Not f Is Nothing Then
                Debug.Print "Found header '" & T & "' at " & f.Address
                f.EntireColumn.Interior.Color = vbRed  '<< for testing
                'f.EntireColumn.Delete                 '<< uncomment when done testing
            End If   'was found
        End If       'any heading
     Next r          'next in list

End Sub

关于excel - 查找有时会生成运行时错误 91,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56794924/

相关文章:

vba - 使用命令按钮将 Excel 工作表发布为 HTML 文件

sql-server - 使用 ADODB 中的变量从 SQL Server 存储过程返回结果

vba - 从文本文件中读取数据并分隔

excel - 使用来自固定列引用但来自当前事件行的内容动态更新单个单元格

excel - 有人可以提供为什么 `Interior.Color` 属性不能在 Excel VBA 中从一个范围分配到另一个范围的技术原因吗?

vba - 在不打开工作簿的情况下从多个工作簿复制特定工作表

vba - 使用单元格引用作为自动形状线

vba - 搜索完全相同的日期或过去最接近的日期的函数 + 一个额外条件

regex - VBA正则表达式问题

c# - 当工作表名称包含空格时无法从 Excel 工作表中读取