[RESOLVED] HELP with FindNext(After)
This question is indirectly related to Excel so I hope I'm posting in the right place. This is actually in a desktop-based application. I'm trying to retrieve information from an Excel document where the information will not always be in the same place.
This is what I have:
Code:
Dim Excel As Object = CreateObject("Excel.Application")
Dim WorkBook As Object = Excel.Workbooks.Open(filePath)
Dim WorkSheet As Object = WorkBook.Worksheets(1)
WorkBook = GetObject(filePath)
WorkSheet = WorkBook.Worksheets(1)
'Determine the name of the first account and the range of rows where it's transactions are.
WorkSheet.Cells.Find("Account name: ").Activate
Dim Acnt1 As Integer = Excel.ActiveWindow.ActiveCell.row()
Dim Acnt2 As Integer = WorkSheet.Cells.FindNext(After:=Excel.ActiveWindow.ActiveCell).row()
My problem is with the FindNext(After:=Excel.ActiveWindow.ActiveCell) part. I have tried telling it to use the active cell among other things but it still gives me the error "unable to get the FindNext property of the range class". It works find without the After property specified but then it always brings up the first instance of what I'm looking for. I tried starting a new Find specifying the after property appropriately and had the same problem. Does anyone have any other suggestions as to why it's not working?
Thanks,
--Rikki
Re: HELP with FindNext(After)
from msdn
Quote:
FindNext Method Example
This example finds all cells in the range A1:A500 that contain the value 2 and makes those cells gray.
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.Pattern = xlPatternGray50
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
Re: [RESOLVED] HELP with FindNext(After)
Okay that works, but since I made the cell that would correspond to c active in the find method 3 lines previous, don't they end up being the same thing?
Thanks,
--Rikki
Re: [RESOLVED] HELP with FindNext(After)
you should avoid using select, selection or active in macros as far as possible, use fully qualified ranges
do not use reserved words or objects as variables
Quote:
Dim WorkBook As Object = Excel.Workbooks.Open(filePath)
Dim WorkSheet As Object = WorkBook.Worksheets(1)
whether this works or does not give error, it will still be likely to cause serious problem at some point
you do not specify what version of excel you are using, or if your code is run from within excel or some scripting