Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA 7.1 Selection.Find Throwing an Error

Status
Not open for further replies.

BeckyMc

Technical User
Jun 19, 2001
51
0
0
US
I don't know why I'm getting an error at the line that says Set RgFound
Code:
Sub CheckFileFormat()

Dim Array1(200) As String
Dim Array2(200) As String
Dim CurrFileName As String
Dim Numrows As Integer
Dim I As Integer

Dim rgFound As Range

CurrFileName = Range("A2").Value
MsgBox CurrFileName

If Range("F2").Value <> "" Then

    ActiveWorkbook.Sheets("ColumnHeadings").Activate
    
    Range("A1").End(xlToRight).Select
    
    Set rgFound = Range(Selection).Find(CurrFileName)
    Range(FoundCell.End(xlDown)).Select
    Numrows = Selection.Rows.Count
    
    For I = 1 To Numrows
    
    
    Next I

End If


End Sub

[code]
 
Hi,

Selection is a Range object, so...
Code:
Set rgFound = Selection.Find(CurrFileName)

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Note that:
1) you refer to non-initialised object. After [tt]Set rgFound = Selection.Find(CurrFileName)[/tt] you call FoundCell: [tt]Range(FoundCell.End(xlDown)).Select[/tt].
2) if rgFound and FoundCell are to be the same cells, then Numrows = Selection.Rows.Count =1, always, as you search and select cells in single row,
3) if nothing found in selected range, the [tt]Select[/tt] method returns nothing, so execune the rest of code conditionally: [tt]In Not rgFound Is Nothing Then[/tt].



combo
 
Thank you! This is working

Code:
Sub CheckFileFormat()

Dim Array1(200) As String
Dim Array2(200) As String
Dim CurrFileName As String
Dim Numrows As Integer
Dim I As Integer

Dim rgFound As Range

CurrFileName = Range("A2").Value


If Range("F2").Value <> "" Then

    ActiveWorkbook.Sheets("ColumnHeadings").Activate
    
    Range("A1").End(xlToRight).Select
    
    Set rgFound = Selection.Find(CurrFileName)
    Range(rgFound, rgFound.End(xlDown)).Select
    Numrows = Selection.Rows.Count
    
    For I = 1 To Numrows
    
    
    Next I

End If


End Sub

[code]
 
Becky,
Please use the Preview button before Submitting your post(s).


---- Andy

There is a great need for a sarcasm font.
 
I'd do it without Activate and Select...
Code:
If Range("F2").Value <> "" Then

   With ActiveWorkbook.Sheets("ColumnHeadings")

      Set rgFound = .Range("A1").End(xlToRight).Find(CurrFileName)

      If Not rgFound Is Nothing Then

         NumRows = .Range(rgFound, rgFound.End(xlDown)).Rows.Count

         For I = 1 To Numrows


         Next 

      End If 

   End With

End If

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top