Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vba - Run-time error 91 using Range.find

SOLVED Nathan_SAV had the right idea. For anyone looking at this for reference later I changed the code to

For x = 3 To lRow
On Error Resume Next
'If then Find statement
If Err.Number <> 0 Then
'....
End If

I'm still new to VBA and I hit an error and I can't figure out how to fix it.

I am getting a "run-time error '91' Object variable or With block variable not set"

On the "Get data here" sheet I have numbers in Column H that I need to transfer to the "Missing Data" sheet. This code is supposed to look at the "Missing Data" sheet and find blanks in column H. If it does find a blank it searches "Get data here" for the value of the cell in Column A on "Missing Data". Then it takes the value from "Get data here" and places it into "Missing Data". I realize this description is confusing but if you look at the code the comments should help.

When I get the run time error it is highlighting S = Range("A:A")....

If anyone could help me out I would appreciate it.

    Dim x As Integer
    Dim lRow As Long, S As Long

    Sheets("Missing Data").Activate
    'Find last row in Missing Data
    lRow = Cells.Find(What:="*", _
        After:=Range("A1"), _
        LookAt:=xlPart, _
        LookIn:=xlFormulas, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, _
        MatchCase:=False).Row

    For x = 3 To lRow
        'Search for all cells without an SAP#
        If Cells(x, 8) = "" Then
            Sheets("Get data here").Activate
            Range("A1").Activate
            'Set S equal to the row where the SQP# was found
            S = Range("A:A").Find(What:=Sheets("Missing Data").Cells(x, 1), _
                After:=ActiveCell, _
                LookAt:=xlWhole, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False).Row
            'Change the value of the blank cell to the value in column H
            Sheets("Missing Data").Cells(x, 8).Value = Sheets("Get data here").Cells(S, 8)
            'Change the cell color to yellow if it changed
            If Sheets("Missing Data").Cells(x, 8) <> "" Then
                Sheets("Missing Data").Cells(x, 8).Interior.ColorIndex = 6
            End If
        End If
    Next x
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The data may not be found, so try

Dim S as long
Dim R as excel.range 
set R=Range("A:A").Find
if R is nothing then 
    'Not found, handle
else
    S=R.row
end if

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...