+ Reply to Thread
Results 1 to 2 of 2

Thread: Using VB To Find a String in an Excel Worksheet

  1. #1
    Registered User
    Join Date
    08-27-2010
    Location
    Yakima, WA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Using VB To Find a String in an Excel Worksheet

    I am working on an application where I need to locate a range of cells within a worksheet. Since the actual location of the data will vary for each worksheet, I have come to the conclusion that I can put a string near the range that I am looking for (i.e. "*SUMMARY*") and since I will know the location of the data that I am looking for relative to the location of this text string, it should work.

    I have made some progress but am having trouble getting the Find method of the range component to work. My code is below. This code find each worksheet and shows me the name but I also need to to find the location of the cell with the string "*SUMMARY*" in it.

    Any suggestions would be appreciated.

    Thanks.

    John

    Imports Microsoft.Office.Interop.Excel
    
    
    Public Class Form1
        Dim oExcelApp As New Microsoft.Office.Interop.Excel.Application
        Dim oWBa As Microsoft.Office.Interop.Excel.Workbook = oExcelApp.Workbooks.Open("C:\Temp\Care Center.xls")
        Dim oWS As Microsoft.Office.Interop.Excel.Worksheet
        Dim SearchArea As Range
    
        Sub ImportRanges()
    
            Dim StartCell As String
    
            oExcelApp.Visible = False
    
            For i = 1 To oWBa.Sheets.Count
                Debug.WriteLine("SheetName: " + oWBa.Sheets(i).name)
                oWS = oWBa.Sheets(oWBa.Sheets(i).name)
                SearchArea = oWS.Range(oWBa.Sheets(i).name).Select()
                StartCell = FindSummary()
                Debug.WriteLine("Summary Location: " + StartCell)
            Next
    
        End Sub
        Public Function FindSummary() As String
    
            Dim currentFind As Range = Nothing
            Dim firstFind As Range = Nothing
    
            ' You should specify all these parameters every time you call this method, 
            ' since they can be overridden in the user interface.
            'currentFind = oWS.Find("*SUMMARY*", , XlFindLookIn.xlValues, XlLookAt.xlPart, XlSearchOrder.xlByRows, XlSearchDirection.xlNext, False)
            currentFind = SearchArea.Find("*SUMMARY*")
    
            While Not currentFind Is Nothing
    
                ' Keep track of the first range you find.
                If firstFind Is Nothing Then
                    firstFind = currentFind
    
                    ' If you didn't move to a new range, you are done.
                ElseIf currentFind.Address = firstFind.Address Then
                    Exit While
                End If
    
                currentFind = oWS.FindNext(currentFind)
    
            End While
    
            Return CStr(currentFind.Row) + CStr(currentFind.Column)
    
        End Function
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ImportRanges()
    
        End Sub
    End Class
    Last edited by shg; 08-28-2010 at 02:50 PM.

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Using VB To Find a String in an Excel Worksheet

    Is that VB? We don't do a lot of that here.

    It doesn't appear that SearchArea is in scope in FindSummary. Is there a compiler directive to require variable declaration?

    EDIT: I retract that. Sorry, I have nothing to contribute.
    Last edited by shg; 08-28-2010 at 03:04 PM.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0