Results 1 to 21 of 21

Thread: Find Next in a Search

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Question Find Next in a Search

    Hey everyone,

    I'm using this nice bit of code from "http://www.authorcode.com/search-text-in-your-excel-file-or-sheet-in-vb-net/"

    It creates a search using a specified word
    Code:
    Private Sub SearchText()
            Dim oXL As Excel.Application
            Dim oWB As Excel.Workbook
            Dim oSheet As Excel.Worksheet
            Try
                Dim File_name As String = "C:\test.xls"
                oXL = CreateObject("Excel.Application")
                oWB = oXL.Workbooks.Open(File_name)
                oSheet = oWB.Worksheets(1)
     
                Dim oRng As Excel.Range = GetSpecifiedRange("get", oSheet)
                If oRng IsNot Nothing Then
                    MessageBox.Show("Text found, position is Row-" & oRng.Row & " and column-" & oRng.Column)
                Else
                    MessageBox.Show("Text is not found")
                End If
                oWB.Close()
     
                oSheet = Nothing
                oWB = Nothing
                oXL.Quit()
     
            Catch ex As Exception
                If oSheet IsNot Nothing Then
                    oSheet = Nothing
                End If
                If oWB IsNot Nothing Then
                    oWB = Nothing
                End If
                If oXL IsNot Nothing Then
                    oXL.Quit()
                End If
            End Try
        End Sub
    Which uses this function:

    Code:
      Private Function GetSpecifiedRange(ByVal matchStr As String, ByVal objWs As Excel.Worksheet) As Excel.Range
            Dim currentFind As Excel.Range = Nothing
            Dim firstFind As Excel.Range = Nothing
            currentFind = objWs.Range("A1:AM100").Find(matchStr, , _
    Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, _
    Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, False)
            Return currentFind
        End Function
    It all works rather well, but the webpage mentions the use of being able to "find next" and "find previous".

    I've been trying for a while and can't figure out how to do it. Any Help?

    Thanks
    VS 2017 - Need as much help as you can give (write it all for me!)

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Hi,

    I posted this the other day for somebody else, hope it helps

    Code:
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            
            Dim xlApp As New Application
            Dim xlWorkbook As Workbook
            Dim xlWorksheet As Worksheet
            xlWorkbook = xlApp.Workbooks.Open("C:\TestExcelToAccess.xls")
            xlWorksheet = CType(xlWorkbook.Worksheets("Sheet1"), Excel.Worksheet)
    
            'find the Last row:
            TextBox1.Text = xlWorkbook.Sheets(1).Cells(xlWorksheet.Rows.Count, 1).End(-4162).Row()
    
            'Test search A1:C Value from Textbox1
            Dim suchRng As Excel.Range = xlWorksheet.Range("A1:" & "C" & TextBox1.Text)
            Dim Row, Col As Long
            For Each rng As Excel.Range In suchRng
                If String.IsNullOrEmpty(rng.Value.ToString) Then Exit For
                If rng.Value.ToString = txtSearch.Text Then
                    'add the Cell and the Text to Listbox
                    ListBox1.Items.Add(rng.Address & " " & rng.Offset(Row, Col).Value)
                End If
            Next
            xlWorksheet = Nothing
            xlWorkbook = Nothing
            xlApp.Quit()
            xlApp = Nothing
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    Does that enable the user to see the next search result? I can't see if it does :/

    Also, if it helps. My "next" and "previous" are on buttons so the user has to decide to want to see the next result.

    Thanks again
    VS 2017 - Need as much help as you can give (write it all for me!)

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Hi,

    it will return all found Cells(in Listbox) with the searchtext that was entered in txtSearch.
    I did not use a Button "next" and "previous"

    Edit:
    what if the entered searchtext would result in 45 find's
    do you really want the user to click 45 times "next" and "previous" ?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    Ahh, I see.. interesting I'll give it a try.
    VS 2017 - Need as much help as you can give (write it all for me!)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    Its for a stock list so that scenario is unlikely bit still very true.

    Im getting System.NullReferenceException on both if statements.

    Code:
    Dim suchRng As Excel.Range = oSheet.Range("B1:B5500")
                    Dim Row, Col As Long
                    For Each rng As Excel.Range In suchRng
                        If String.IsNullOrEmpty(rng.Value.ToString) Then Exit For
                        If rng.Value.ToString = Form1.TextBox2.Text Then
                            'add the Cell and the Text to Listbox
                            ListBox1.Items.Add(rng.Address & " " & rng.Offset(Row, Col).Value)
                        End If
                    Next
    VS 2017 - Need as much help as you can give (write it all for me!)

  7. #7
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Find Next in a Search

    Refer to post #8 in your other thread on this topic. The error is similar.

    http://www.vbforums.com/showthread.p...rch-A-Workbook
    Please remember next time...elections matter!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    So its the "value" being empty? What do I need to put in it
    VS 2017 - Need as much help as you can give (write it all for me!)

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Hi,

    well evan if a certain Product in Stock is empty it should be 0 and not Blank

    well anyway try this change
    Code:
     Dim suchRng As Excel.Range = xlWorksheet.Range("A1:" & "C" & TextBox1.Text)
            Dim Row, Col As Long
            Dim s As String
    Edit: sorry this can go
           ' s = Nothing
            For Each rng As Excel.Range In suchRng
                If String.IsNullOrEmpty(rng.Value) Then
                    rng.Value = Nothing
                    ListBox1.Items.Add(rng.Address & "Empty " & rng.Offset(Row, Col).Value) 'add the Cell and state it's empty
                ElseIf rng.Value.ToString = txtSearch.Text Then
                    'add the Cell and the Text to Listbox
                    ListBox1.Items.Add(rng.Address & " " & rng.Offset(Row, Col).Value)
                End If
            Next
    regards
    Chris
    Last edited by ChrisE; Sep 12th, 2017 at 06:19 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    I don't think its searching properly... Might have to revert back to the "Find Next" option so I don't loose my sanity haha.
    VS 2017 - Need as much help as you can give (write it all for me!)

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Hi,

    well I tried it
    I changed a couple of Cells in Excel to Blank Cells
    and it returned my Searchtext and also added the empty cells to the Listbox

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    mine returns every blank cell and ignores every cell with data in
    VS 2017 - Need as much help as you can give (write it all for me!)

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Quote Originally Posted by callumwk View Post
    mine returns every blank cell and ignores every cell with data in
    well then you are doing something wrong

    here again with changes
    Code:
      Dim xlApp As New Application
            Dim xlWorkbook As Workbook
            Dim xlWorksheet As Worksheet
            xlWorkbook = xlApp.Workbooks.Open("C:\TestExcelToAccess.xls")
            xlWorksheet = CType(xlWorkbook.Worksheets("Sheet1"), Excel.Worksheet)
    
            'find the Last row:
            TextBox1.Text = xlWorkbook.Sheets(1).Cells(xlWorksheet.Rows.Count, 1).End(-4162).Row()
    
            'Test search A1:C Value from Textbox1
            Dim suchRng As Excel.Range = xlWorksheet.Range("A1:" & "C" & TextBox1.Text)
            Dim Row, Col As Long
    
            For Each rng As Excel.Range In suchRng
                If String.IsNullOrEmpty(rng.Value) Then
                    rng.Value = Nothing
                    ListBox1.Items.Add(rng.Address & "Empty " & rng.Offset(Row, Col).Value)
                ElseIf rng.Value.ToString = txtSearch.Text Then
                    'add the Cell and the Text to Listbox
                    ListBox1.Items.Add(rng.Address & " " & rng.Offset(Row, Col).Value)
                End If
            Next
            xlWorksheet = Nothing
            xlWorkbook = Nothing
            xlApp.Quit()
            xlApp = Nothing
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    Code:
    'find the Last row:
                    TextBox5.Text = oWB.Sheets(1).Cells(oSheet.Rows.Count, 1).End(-4162).Row()
    
                    'Test search A1:C Value from Textbox1
                    Dim suchRng As Excel.Range = oSheet.Range("A1:" & "C" & TextBox5.Text)
                    Dim Row, Col As Long
    
                    For Each rng As Excel.Range In suchRng
                        If String.IsNullOrEmpty(rng.Value) Then
                            rng.Value = Nothing
                            ListBox1.Items.Add(rng.Address & "Empty " & rng.Offset(Row, Col).Value)
                        ElseIf rng.Value.ToString = Form1.textbox2.Text Then
                            'add the Cell and the Text to Listbox
                            ListBox1.Items.Add(rng.Address & " " & rng.Offset(Row, Col).Value)
                        End If
                    Next
    Text box 5 =
    the last row containing text



    list box =
    Name:  Capture1.PNG
Views: 1046
Size:  8.1 KB

    the spreadsheet contains stock codes in column A and descriptions in B. I only want to search for matches in column B, but as you can see the list box only shows empty cells.
    Attached Images Attached Images  
    Last edited by callumwk; Sep 12th, 2017 at 08:33 AM.
    VS 2017 - Need as much help as you can give (write it all for me!)

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Quote Originally Posted by callumwk View Post
    Code:
    'find the Last row:
                    TextBox5.Text = oWB.Sheets(1).Cells(oSheet.Rows.Count, 1).End(-4162).Row()
    
                    'Test search A1:C Value from Textbox1
                    Dim suchRng As Excel.Range = oSheet.Range("A1:" & "C" & TextBox5.Text)
                    Dim Row, Col As Long
    
                    For Each rng As Excel.Range In suchRng
                        If String.IsNullOrEmpty(rng.Value) Then
                            rng.Value = Nothing
                            ListBox1.Items.Add(rng.Address & "Empty " & rng.Offset(Row, Col).Value)
                        ElseIf rng.Value.ToString = Form1.textbox2.Text Then
                            'add the Cell and the Text to Listbox
                            ListBox1.Items.Add(rng.Address & " " & rng.Offset(Row, Col).Value)
                        End If
                    Next

    Text box 5 = the last row containing text



    list box =
    Name:  Capture1.PNG
Views: 1046
Size:  8.1 KB

    the spreadsheet contains stock codes in column A and descriptions in B. I only want to search for matches in column B, but as you can see the list box only shows empty cells.
    I don't understand your Textbox5 =
    Text box 5 = the last row containing text
    in Textbox5 should be a number (number of rows in you Excelsheet)

    I think I will upload my sample and you can see better.

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    Quote Originally Posted by ChrisE View Post
    I don't understand your Textbox5 =

    in Textbox5 should be a number (number of rows in you Excelsheet)

    I think I will upload my sample and you can see better.

    regards
    Chris
    That's what I'm saying. Text box 5 contains the number of the last row containing text. I.E, the number of rows on the sheet.
    VS 2017 - Need as much help as you can give (write it all for me!)

  17. #17
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Hi,

    here a screenshot of my result. I have 78 rows in my Excelfile, you can see the
    search text and the result in the listbox

    Name:  sample Excel.JPG
Views: 1100
Size:  15.0 KB


    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    I understand your search is searching for exact terms. I have made some adjustments:
    Code:
     Dim rownum As Integer = oWB.Sheets(1).Cells(oSheet.Rows.Count, 1).End(-4162).Row()
                    Dim suchRng As Excel.Range = oSheet.Range("B3:" & "B" & rownum)
                    Dim Row, Col As Long
    
                    For Each rng As Excel.Range In suchRng
                        If String.IsNullOrEmpty(rng.Value) Then
                            rng.Value = Nothing
                            ListBox1.Items.Add(rng.Address & "Empty " & rng.Offset(Row, Col).Value)
                        ElseIf InStr(LCase(rng.Value.ToString), Form1.TextBox2.Text, CompareMethod.Text) <> 0 Then
                            'add the Cell and the Text to Listbox
                            ListBox1.Items.Add(rng.Address & " " & rng.Offset(Row, Col).Value)
                        End If
                    Next
    VS 2017 - Need as much help as you can give (write it all for me!)

  19. #19
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    working now ?
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    123

    Re: Find Next in a Search

    Indeed, is it just me or does your profile picture keep changing?
    VS 2017 - Need as much help as you can give (write it all for me!)

  21. #21
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Find Next in a Search

    Quote Originally Posted by callumwk View Post
    Indeed, is it just me or does your profile picture keep changing?
    Hi,
    glad you got it working, I change my Profil picture a couple of days ago.

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Tags for this Thread

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