|
-
Oct 28th, 2008, 01:36 PM
#1
Thread Starter
Addicted Member
[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
-
Oct 28th, 2008, 03:43 PM
#2
Re: HELP with FindNext(After)
from msdn
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
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
-
Oct 28th, 2008, 03:48 PM
#3
Thread Starter
Addicted Member
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
-
Oct 28th, 2008, 03:55 PM
#4
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
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
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
|