Results 1 to 12 of 12

Thread: [RESOLVED] Trying to search excel and return a count number, any ideas to help jump start me?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Resolved [RESOLVED] Trying to search excel and return a count number, any ideas to help jump start me?

    I can open and read and write to excel, but I need some help on how to search through column A to find a particular text such as B22-11, once found then I need to get the row number (count). If anyone here can help jump start me on this that would be great.

    Thanks.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    What do you have so far? Really all you will be doing is a for loop incrementing the row number. Is this what you need help with?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    Quote Originally Posted by ForumAccount View Post
    What do you have so far? Really all you will be doing is a for loop incrementing the row number. Is this what you need help with?
    Well I am using some of the code that you had help me on a week or so ago. I am just not sure what the key item names are for these in the if statement:

    vb.net Code:
    1. Imports Excel = Microsoft.Office.Interop.Excel
    2.  
    3. Public Class myClass
    4.  
    5.   Private Sub UpdateBay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateBay.Click
    6.  
    7.        
    8.  
    9.     Dim myPath As String = ("C:\Test.xls")
    10.     Dim xlApp As Excel.Application = New Excel.ApplicationClass
    11.     Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(myPath)
    12.     Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Worksheets("Status")
    13.     Dim myDate As DateTime = Date.Now
    14.  
    15.         If Collum A = some_variable_String Then
    16.        get row number/count and set it to variable myCount
    17.        MsgBox(xlWorkSheet.Range("A" & myCount).Value)
    18.        xlWorkSheet.Range("J" & myCount).Value = "Done " & myDate.Date
    19.        xlWorkSheet.Range("A" myCount:M" & myCount).Interior.ColorIndex = 35
    20.  
    21.         Else
    22.  
    23.     Increment down one cell?
    24.  
    25.  
    26.         xlWorkBook.Close()
    27.         xlApp.Quit()
    28.         releaseObject(xlApp)
    29.         releaseObject(xlWorkBook)
    30.         releaseObject(xlWorkSheet)
    31.  
    32.     End Sub
    33.  
    34.    Private Sub releaseObject(ByVal obj As Object)
    35.         Try
    36.             System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
    37.             obj = Nothing
    38.         Catch ex As Exception
    39.             obj = Nothing
    40.         Finally
    41.             GC.Collect()
    42.         End Try
    43.     End Sub
    44.  
    45. End Class

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    You could do this;

    Code:
            Dim LastRow As Integer = xlSheet.UsedRange.Rows(xlSheet.UsedRange.Rows.Count).Row
            Dim LastCol As Integer = xlSheet.UsedRange.Columns(xlSheet.UsedRange.Columns.Count).Column
    
            'Assuming ColumnNumber is the index of the column youre searching
            For i As Integer = 1 To LastRow
                If Not IsNothing(xlSheet.Application.Cells(i, ColumnNumber).Value) _
                AndAlso xlSheet.Application.Cells(i, ColumnNumber).Value.ToString = "A_String" Then
                    'Do something
                End If
            Next
    The row number will be the value of i when the string is found. If the string occurs more than once then you'll need to detect that.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    OK I understand half of this but I am confused about two things:

    What is "A_String" is this what I am searching for?

    and what is ColumnNumber?

    I am looking to search column "A" and I am looking for a certain string.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    OK I got it now, but I need to figure out how to exit out of that for loop once I find something.

    vb.net Code:
    1. Public Class mySearch
    2.  
    3.     Private Sub UpdateBay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateBay.Click
    4.  
    5.         Dim myPath As String = ("C:\Test.xls")
    6.         Dim xlApp As Excel.Application = New Excel.ApplicationClass
    7.         Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(myPath)
    8.         Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Worksheets("Status")
    9.         Dim myDate As DateTime = Date.Now
    10.  
    11.         Dim mySearch As String = ("BP-" & TextBox1.Text)
    12.         Dim LastRow As Integer = xlWorkSheet.UsedRange.Rows(xlWorkSheet.UsedRange.Rows.Count).Row
    13.         Dim LastCol As Integer = xlWorkSheet.UsedRange.Columns(xlWorkSheet.UsedRange.Columns.Count).Column
    14.  
    15.         For i As Integer = 1 To LastRow
    16.             If Not IsNothing(xlWorkSheet.Application.Cells(i, "A").Value) _
    17.             AndAlso xlWorkSheet.Application.Cells(i, "A").Value.ToString = mySearch Then
    18.                 'Do something
    19.                 MsgBox(xlWorkSheet.Range("A" & i).Value)
    20.                 xlWorkSheet.Range("J" & i).Value = "Done " & myDate.Date
    21.                 xlWorkSheet.Range("M" & i).Value = "Sent " & myDate.Date
    22.                 'xlWorkSheet.Range("A89:M89").Interior.ColorIndex = 35
    23.             End If
    24.         Next
    25.  
    26.  
    27.         xlWorkBook.Close()
    28.         xlApp.Quit()
    29.         releaseObject(xlApp)
    30.         releaseObject(xlWorkBook)
    31.         releaseObject(xlWorkSheet)
    32.  
    33.  
    34.  
    35.     End Sub

  7. #7
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Trying to search excel and return a count number, any ideas to help jump start me


  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    Any Idea how I could do this, bellow but with a variable?
    Code:
    'xlWorkSheet.Range("A89:M89").Interior.ColorIndex = 35

    N/G
    Code:
    xlWorkSheet.Range("A"&i):("M"&i).Interior.ColorIndex = 35
    N/G
    Code:
    xlWorkSheet.Range("A"&i:"M"&i).Interior.ColorIndex = 35
    Thanks.

  9. #9
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    vb.net Code:
    1. ws.Range("A" & i & ":M" & i).Interior.ColorIndex = 35

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    Thanks! How did ya figure that one out?


  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    OK, one last problem I tried to add this to give me a message that string not found and it only half works that is if my search string is found it then also says not found. If the string is not found then it seems to work. Why is it that when a string is found both for loops execute?

    vb.net Code:
    1. For i As Integer = 1 To LastRow
    2.             If Not IsNothing(xlWorkSheet.Application.Cells(i, "A").Value) _
    3.             AndAlso xlWorkSheet.Application.Cells(i, "A").Value.ToString = mySearch Then
    4.                 'Do something
    5.                 MsgBox(xlWorkSheet.Range("A" & i).Value)
    6.                 xlWorkSheet.Range("J" & i).Value = "Done " & myDate.Date
    7.                 xlWorkSheet.Range("M" & i).Value = "Sent " & myDate.Date
    8.                 xlWorkSheet.Range("A" & i & ":M" & i).Interior.ColorIndex = 35
    9.             End If
    10.             'MsgBox(LastRow)
    11.         Next
    12.  
    13.         For i As Integer = 1 To LastRow
    14.             If Not IsNothing(xlWorkSheet.Application.Cells(i, "A").Value) _
    15.             AndAlso xlWorkSheet.Application.Cells(i, "A").Value.ToString <> mySearch Then
    16.                 'Do something
    17.                 MsgBox(mySearch & "  Not Found")
    18.                 Exit For
    19.             End If
    20.  
    21.         Next

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Location
    Boston, MA
    Posts
    226

    Re: Trying to search excel and return a count number, any ideas to help jump start me

    Well my original goal here has been solved thanks to all the great posts and ideas you guys gave me! Thanks to all.


Posting Permissions

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



Click Here to Expand Forum to Full Width