+ Reply to Thread
Results 1 to 8 of 8

Ways to expand search bar code? Thanks!!!

Hybrid View

  1. #1
    Registered User
    Join Date
    07-09-2017
    Location
    United Kingdom
    MS-Off Ver
    2010
    Posts
    39

    Ways to expand search bar code? Thanks!!!

    Hi all,

    I have made a search bar on a userform that will be used for finding data values across the workbook from different worksheets.

    So far the search bar can only find values from "Sheet3". I want to modify the code so it can pick up data values from all worksheets in the workbook,

    The search bar pickup values regarding to range and displays each row of data within 15 different TxtBoxes.

    Below is the code I have used and a screenshot of my user form.


    Private Sub btnSearch_Click()
    Dim ws As Worksheet
    Dim strSearch As String
    Dim aCell As Range, v
    
    On Error GoTo Err
    
    'validate text box
    v = Trim(TextBox6.Value)
    If Len(v) = 0 Then
        MsgBox "Please Enter Data Value."
        Cancel = True
        Me.TextBox6.SetFocus
        Exit Sub
    End If
    
    Set aCell = Sheets("sheet3").Range("A:A").Find(v, lookat:=xlWhole)
    
    If Not aCell Is Nothing Then
    
        With aCell.EntireRow
    
            TextBox1.Text = .Cells(, "A").Value
            TextBox2.Text = .Cells(, "B").Value
            TextBox3.Text = .Cells(, "C").Value
            TextBox4.Text = .Cells(, "D").Value
            TextBox5.Text = .Cells(, "E").Value
            TextBox7.Text = .Cells(, "F").Value
            TextBox8.Text = .Cells(, "G").Value
            TextBox9.Text = .Cells(, "H").Value
            TextBox10.Text = .Cells(, "I").Value
            TextBox11.Text = .Cells(, "J").Value
            TextBox12.Text = .Cells(, "K").Value
            TextBox13.Text = .Cells(, "L").Value
            TextBox14.Text = .Cells(, "M").Value
            TextBox15.Text = .Cells(, "N").Value
     
           
    
        End With
    
    Else
        MsgBox "Data Value Not Found."
        Cancel = True
        'frmSearchTest.TextBox6.Value = ""
        TextBox6.SetFocus
    End If
    
    Exit Sub
    
    Err:
        MsgBox Err.Description
    
    End Sub
    Attached Images Attached Images

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,260

    Re: Ways to expand search bar code? Thanks!!!

    Hi Kamal,

    You need to expand your search to multiple worksheets using the Worksheet INDEX.
    In VBA WorkSheets.Count is how many sheets you have and instead of "Sheets("Sheet3")" you need to loop through all your sheets. Something like:

    Dim SheetCounter as Dounble
    ...
    For SheetCounter = 3 to Sheets.Count
    Set aCell = Sheets(SheetCounter).Range("A:A").Find(v, lookat:=xlWhole)
    ...
    ...
    Next SheetCounter
    ALSO you need to have your code in a MODULE that isn't behind a sheet or the userform as this will allow it to scope each sheet correctly. You have you Sub as Private and this might mean it can't look outside of the sheet module it is behind.

    http://www.cpearson.com/excel/Scope.aspx
    Last edited by MarvinP; 10-10-2017 at 12:00 PM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Ways to expand search bar code? Thanks!!!

    You know that functionality is built into Excel?

    Marvin, Marvin, Marvin -- where are your CODE tags?
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,260

    Re: Ways to expand search bar code? Thanks!!!

    @shg,

    OK - code tags in place,,,

    I hate sentences that start with "You Know" as much as I hate sentences that start with "Unfortunately"

    What functionality are you speaking of that "is built into Excel"?

    Note - as I get older it seems I know less and less. Is it there is much more to know or I'm loosing some of my marbles?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Ways to expand search bar code? Thanks!!!

    I confess to only glancing at the form, and not reading the OP's code (or yours) at all, but ...

    If you do Find, set the Within dropdown to Workbook, and press Find All, you get a nice list of all the places the search value was found.

    >> I hate sentences that start with "You Know"

    The comment was directed to the OP, young Marvin

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,260

    Re: Ways to expand search bar code? Thanks!!!

    @shg
    young Marvin

    I may have a few years on ya, shg (dob in 47 is me)

    Also - I don't use VBA Find often and will need to lookup the parms. I don't see "Within.Workbook" as one of the arguments to the VBA Find Method....

    After searching more you meant to use the Home Tab - Find and Select Icon - Find .. Which shows the Find and Replace Dialog where I find the Within Workbook stuff.

    shg - I see now where you were directing me...

    I didn't know the "Find All" button list was capturable into VBA. Is it?

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Ways to expand search bar code? Thanks!!!

    I didn't know the "Find All" button list was capturable into VBA. Is it?
    It's not, and you also can't control the Sheet/Workbook selector. But my point was/is, why create a userform and write a bunch of code to do something that the UI already does just fine.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,260

    Re: Ways to expand search bar code? Thanks!!!

    NOW I totally understand!! Thanks for clearing that up

    I guess I just missed the OP's intent and that it is already built into the GoTo tool in Excel. (I'm still learning )

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Expand 'Search' to 2 columns
    By JO505 in forum Excel General
    Replies: 5
    Last Post: 11-23-2015, 05:08 AM
  2. Replies: 1
    Last Post: 05-24-2013, 06:02 AM
  3. [SOLVED] Ways to make my code run faster
    By JazzyBear in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-03-2013, 03:59 PM
  4. [SOLVED] fastest ways to make a search
    By yberf in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2012, 11:08 AM
  5. WithEvents - same code run 2 ways: one causes crash
    By Deskmanbb in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-29-2011, 03:40 PM
  6. What are the good ways to write VBA code,
    By malviya_anil77 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-24-2011, 03:03 PM
  7. Two ways to search
    By 1000xx in forum Excel General
    Replies: 0
    Last Post: 05-21-2011, 04:48 AM

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