Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net Excel Add-in Find values on sheet and don't go past the end of the sheet

Hi
I am using the following VB.net code in my Excel Add-in to find all instances of a word on a spreadsheet.
The loop doesn't end at the end of the sheet  but rather just carries in. What extra code can I include
to not go past the end of the sheet?
                    
Dim blnFindError As Boolean = False

                    oFindWhat = "Total"
                    strSourceRange = oSourceWorkBook.Sheets(oSourceSheet).Cells.Address

                    Do Until blnFindError = True

                        Try
                            oFindAddress = oSourceWorkBook.Sheets(oSourceSheet).Range(strSourceRange).Find(
        What:=oFindWhat, After:=oSourceWorkBook.Sheets(oSourceSheet).Range(oFindAfterAddress),
        LookIn:=Excel.XlFindLookIn.xlValues, LookAt:=Excel.XlLookAt.xlPart, SearchOrder:=oSearchOrder,
        SearchDirection:=Excel.XlSearchDirection.xlNext).Address
                        Catch ex As Exception
                            blnFindError = True
                        End Try

                        oFindAfterAddress = oFindAddress 


                    Loop

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial