+ Reply to Thread
Results 1 to 6 of 6

Search Macro button help

Hybrid View

  1. #1
    Registered User
    Join Date
    06-28-2015
    Location
    North East, England
    MS-Off Ver
    2010
    Posts
    39

    Search Macro button help

    Hi I have a Knowledge database spread sheet that stores defects. It has 4 sheets Instructions, Input, DataSheet and lookuplist. On the Input sheet I have a Search button with VBA code that displays a msgBox and searches for keywords entered then tells you how many it has found. The problem I am having is that I cant work out how to get the code to only search the DataSheet because at present it searches every sheet and I don't want that.

    Please can someone help me. here is the code

    Sub search()
    Dim ws As Worksheet, Found As Range, rngNm As String
     Dim myText As String, FirstAddress As String, thisLoc As String
     Dim AddressStr As String, foundNum As Integer
     
    myText = InputBox("Enter text to find")
     
    If myText = "" Then Exit Sub
     
    For Each ws In ThisWorkbook.Worksheets
     With ws
     Set Found = .UsedRange.Find(what:=myText, LookIn:=xlValues, MatchCase:=False)
     
    If Not Found Is Nothing Then
     FirstAddress = Found.Address
     Do
     foundNum = foundNum + 1
     rngNm = .Name
     AddressStr = AddressStr & .Name & " " & Found.Address & vbCrLf
     thisLoc = rngNm & " " & Found.Address
     
    Sheets("DataSheet").Select
     Range(Found.Address(RowAbsolute:=False, _
     ColumnAbsolute:=False)).Select
     
    MsgBox "Found one """ & myText & """ here!" & vbCr & vbCr & _
     thisLoc
     
    Set Found = .UsedRange.FindNext(Found)
     
    Loop While Not Found Is Nothing And Found.Address <> FirstAddress
     End If
     End With
     
    Next ws
     
    If Len(AddressStr) Then
     MsgBox "Found: """ & myText & """ " & foundNum & " times." & vbCr & _
     AddressStr, vbOKOnly, myText & " found in these cells"
     Else:
     MsgBox "Unable to find " & myText & " in this workbook.", vbExclamation
     End If
     
    End Sub
    Last edited by stanton17; 07-03-2015 at 04:53 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Search Macro button help

    Firstly:
    You will get a lot of abuse for not using code tags.

    edit your post

    select your code and click the # in the menu bar.

    Secondly:

    You have programmed your search routine to search every sheet.

    
    Sub search()
    Dim ws As Worksheet, Found As Range, rngNm As String
    Dim myText As String, FirstAddress As String, thisLoc As String
    Dim AddressStr As String, foundNum As Integer
    
    myText = InputBox("Enter text to find")
    
    If myText = "" Then Exit Sub
    
    with sheets("Datasheet")
    Set Found = .UsedRange.Find(what:=myText, LookIn:=xlValues, MatchCase:=False)
    
    If Not Found Is Nothing Then
    FirstAddress = Found.Address
    Do
    foundNum = foundNum + 1
    rngNm = .Name
    AddressStr = AddressStr & .Name & " " & Found.Address & vbCrLf
    thisLoc = rngNm & " " & Found.Address
    
    Sheets("DataSheet").Select
    Range(Found.Address(RowAbsolute:=False, _
    ColumnAbsolute:=False)).Select
    
    MsgBox "Found one """ & myText & """ here!" & vbCr & vbCr & _
    thisLoc
    
    Set Found = .UsedRange.FindNext(Found)
    
    Loop While Not Found Is Nothing And Found.Address <> FirstAddress
    End If
    End With
    
    If Len(AddressStr) > 0 Then
    MsgBox "Found: """ & myText & """ " & foundNum & " times." & vbCr & _
    AddressStr, vbOKOnly, myText & " found in these cells"
    Else:
    MsgBox "Unable to find " & myText & " in this workbook.", vbExclamation
    End If
    
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    06-28-2015
    Location
    North East, England
    MS-Off Ver
    2010
    Posts
    39

    Re: Search Macro button help

    I apologise for the way I posted the code, I will make sure I use code tags in the future
    Thank you for the code it worked perfectly.

  4. #4
    Registered User
    Join Date
    06-28-2015
    Location
    North East, England
    MS-Off Ver
    2010
    Posts
    39

    Re: Search Macro button help

    xladept

    Thank you for the code

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Search Macro button help

    Hi Stanton,

    Welcome to the Forum!

    Here's a quick fix (see red):

    Sub search()
    Dim ws As Worksheet, Found As Range, rngNm As String
    Dim myText As String, FirstAddress As String, thisLoc As String
    Dim AddressStr As String, foundNum As Integer
    
    myText = InputBox("Enter text to find")
    
    If myText = "" Then Exit Sub
    
    For Each ws In ThisWorkbook.Worksheets
    If ws.name = "Datasheet Name goes here" Then
    With ws
    Set Found = .UsedRange.Find(what:=myText, LookIn:=xlValues, MatchCase:=False)
    
    If Not Found Is Nothing Then
    FirstAddress = Found.Address
    Do
    foundNum = foundNum + 1
    rngNm = .name
    AddressStr = AddressStr & .name & " " & Found.Address & vbCrLf
    thisLoc = rngNm & " " & Found.Address
    
    Sheets("DataSheet").Select
    Range(Found.Address(RowAbsolute:=False, _
    ColumnAbsolute:=False)).Select
    
    MsgBox "Found one """ & myText & """ here!" & vbCr & vbCr & _
    thisLoc
    
    Set Found = .UsedRange.FindNext(Found)
    
    Loop While Not Found Is Nothing And Found.Address <> FirstAddress
    End If
    End With
    End If
    Next ws
    
    If Len(AddressStr) Then
    MsgBox "Found: """ & myText & """ " & foundNum & " times." & vbCr & _
    AddressStr, vbOKOnly, myText & " found in these cells"
    Else:
    MsgBox "Unable to find " & myText & " in this workbook.", vbExclamation
    End If
    
    End Sub
    *Notice that I put code tages around the code - here are instructions for that:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Search Macro button help

    You're welcome and thanks for the rep!

+ 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. Search Button Macro
    By Cremorneguy in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-18-2014, 09:51 PM
  2. Search Button Macro
    By sunshne900 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2013, 03:18 AM
  3. [SOLVED] Macro/s to create Search Bar to filter to search terms entered activated by Command Button
    By JasonRay in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-08-2013, 03:44 PM
  4. how to set a macro for a search button
    By chriford167 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-30-2011, 02:23 AM
  5. Search button using macro
    By Munni in forum Excel General
    Replies: 1
    Last Post: 08-27-2010, 02:46 AM

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