|
-
Aug 29th, 2012, 02:13 PM
#1
Thread Starter
New Member
Excel 2010 - VBA Subscript Out of Range Error - used to work in Excel 2007
I was recently upgraded to Excel 2010 and one of my VBA codes has stopped working (use to run Excel 2007), and I am getting a subscript out of range error on the line in bold below.
Sub Search_Me()
'
'Based on the principle of the Find Method, This program will search for
'a string in the tag name colums of the Final Sortable List Page.
'The results of the search will be displayed by hiding the irrelevant
'data entries. To display the complete list, the User has to hit
'cancel or OK without making any entries to the textbox.
'
'Variable declaration
Dim iRow As Integer 'Number of rows in the dataset.
Dim iCol As Integer 'Column numbers that will be searched.
Dim iI As Integer 'Loop counter.
Dim bRow() As Boolean 'Row Formatting Array (True=Hit on Search Parameter).
Dim SearchString As String 'User's search request.
Dim LookInR As Variant 'Search range.
Dim FoundOne As Variant 'Intermittent search result.
Dim fAddress As Variant 'Address tag for first search result.
'Count number of rows in dataset. Using LOC column as marker
iRow = 6
Do
iRow = iRow + 1
Loop While (Cells(iRow + 1, 1).Value <> "")
ReDim bRow(6 To iRow) As Boolean 'Array redefined based on dataset size.
'Cleanup.
FinalList.Cells(2, 2).Value = Null
Rows("6:" & iRow).Hidden = False
FinalList.Range("B6:B" & iRow & ",E6:E" & iRow & ",L6:L" & iRow).Interior.Color = vbWhite
'Initiate search.
SearchString = Application.InputBox( _
"Enter Search parameter:" & Chr(10) & _
"You can search for a full tag name or a part of it." & Chr(10) & _
"Pressing OK without an entry or Cancel will clear the search results.", _
"Tag Name Search", , 100, 100, , , 2)
'Check for inpaaropriate selections.
If SearchString = "" Or SearchString = "False" Then Exit Sub
'Begin find method loop accross columns.
FinalList.Cells(2, 2).Value = SearchString
For iI = 1 To 5
'Make column selection based on iteration #.
Select Case iI 'Column Headings:
Select Case iI 'Column Headings:
Case 1
iCol = 2 'UNIT
Case 2
iCol = 3 'EQUIPMENT
Case 3
iCol = 6 'TAGNAME 1
Case 4
iCol = 13 'TAGNAME 2
Case 5
iCol = 20 'TAGNAME 3
End Select
'Set the search range based on the selected column.
'Searchable text starts on row 6
Set LookInR = FinalList.Range(Cells(6, iCol), Cells(iRow, iCol))
'Begin Find Method. (referenced from example in Help Files)
With LookInR
Set FoundOne = .Find(What:=SearchString, LookIn:=xlPart) 'Find the first hit.
If Not FoundOne Is Nothing Then
fAddress = FoundOne.Address 'Record the location of the first hit.
'Loop and record hits till the method finds the first hit again.
Do
'FoundOne.Interior.Color = vbYellow ':Used only during debug.
bRow(FoundOne.Row) = True ':Used for data formatting.
Set FoundOne = .FindNext(FoundOne) 'Find the next hit.
Loop While Not FoundOne Is Nothing And FoundOne.Address <> fAddress
End If
End With
Next iI
'Final Data Formatting.
For iI = 7 To iRow
FinalList.Rows(iI).Hidden = Not (bRow(iI)) 'Hide all rows without any hits on them.
Next iI
End Sub
-
Aug 30th, 2012, 03:57 PM
#2
Re: Excel 2010 - VBA Subscript Out of Range Error - used to work in Excel 2007
vba in 2010 may not require the set, keyword, try without it
check if lookinR has returned a range
Code:
if lookinr is nothing then msgbox "not found"
Last edited by westconn1; Aug 30th, 2012 at 04:03 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|