+ Reply to Thread
Results 1 to 3 of 3

VBA search box

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-30-2006
    MS-Off Ver
    O365
    Posts
    311

    VBA search box

    A little while ago i asked for help with a code to search a workbook for a reference number i input into a cell.

    Thanks for this code guys, i just need 1 little change though...

    Private Sub CommandButton1_Click()
    Dim rng, sh As Worksheet
    Dim WhatToFind As String
    Dim Ans As String
    WhatToFind = Range("A12").Value ' "InputYourString" ' adjust to your needs ...
    For Each sh In ActiveWorkbook.Worksheets
    If Not sh.Name = "Front Page" Then
    sh.Select
    With ActiveSheet
    On Error Resume Next
    .Columns("B").Select
    Selection.Find(what:=WhatToFind, _
    After:=ActiveCell, _
    LookIn:=xlFormulas, _
    LookAt:=xlPart, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False).Activate
    If ActiveCell.Row > 6 Then
    Ans = MsgBox("Found at " & ActiveCell.Address & " Continue searching ? ", vbYesNo)
    If Ans = vbNo Then Exit Sub
    End If
    End With

    I want to seach all the workbook apart from the "Front Page". What do i need to add onto this code?

  2. #2
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    This code should search all codes EXCEPT the sheet named "Front Page". What problems are you having with it as written?

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Private Sub CommandButton1_Click()
    Dim rng, sh As Worksheet
    Dim WhatToFind As String
    Dim foundCell As Range
    Dim Ans As String
    WhatToFind = Range("A12").Value ' "InputYourString" ' adjust to your needs ...
    For Each sh In ActiveWorkbook.Worksheets
        If Not sh.Name = "Front Page" Then
        With sh
            Set foundCell = .Range("B:B").Find(what:=WhatToFind, _
                After:=.Range("B1"), _
                LookIn:=xlFormulas, _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False)
            If Not (Nothing Is foundCell) Then
              If foundCell.Row > 6 Then
                Ans = MsgBox("Found on " & sh.Name & " at " & foundCell.Address _
                    & " Continue searching ? ", vbYesNo)
                If Ans = vbNo Then Exit Sub
              End If
            End If
        End With
        End If
    Next sh
    End Sub
    It looks like you chopped off the end of some of your code. If the above code finds WhatToFind on a sheet, it will ask you if you want to keep looking. (I wasn't sure what you wanted at that point because the posted code was truncated.) If you say YES, then it will look at the next sheet. This routine will not find two occurances on the same sheet. This can be easily changed.
    I also removed some unneeded selecting and such. If you want to go to the found cell after pressing NO, replace the above line with
    If Ans = vbNo Then sh.Activate: foundCell.Select: Exit Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1