1

I get "Application-Defined or Object-Defined Error" while I run the below Macro.

I want to parse through each column in "DB" sheets and search for that column.

Sub test()  
Dim FindString As Range
Dim Rng As Range

Dim i, j As Integer
Dim finalcol As Long

Worksheets("DB").Select

finalcol = Worksheets("DB").Cells(1, Application.Columns.Count).End(x1toleft).column
On Error Resume Next

For i = 1 To finalcol
    FindString = Cells(1, i).Value

    If Trim(FindString) <> "" Then
        With Sheets("DB").Range("A:A")
            Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
    End If
Next i
On Error GoTo 0

End Sub
4

1 回答 1

4

你的x1toleft常数应该是xlToLeft(ex ell,而不是 ex one)。它不会转换为驼色外壳的事实是一个提示。

此外,FindString 不应该是Dim FindString As StringRange。如果你摆脱了这On Error Resume Next条线,你会得到一个FindString = Cells(1,i).Value在线错误,因为你必须使用Set对象变量。当它运行并且错误被抑制时,FindString(作为 Range 变量)是 Nothing。

我没有得到你得到的错误,它只是找不到任何东西。但是,如果您进行这些更改,它将修复它或暴露真正的错误。在任何情况下,您都应该删除错误处理,直到您对其进行调试,然后再将其添加回来。

于 2012-12-05T15:43:02.167 回答