+ Reply to Thread
Results 1 to 7 of 7

Why wont my search function work when i change it from all sheets to active sheet?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-25-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2003
    Posts
    179

    Why wont my search function work when i change it from all sheets to active sheet?

    I want this code to execute only for the data in worksheet 'User Sheet' from A2:A1001, however, i cant get it to work. It worked when i was running it for all sheets via the application function.
    Please help

    Sub myfind()
    Dim Message, Title, Default, SearchString
    Message = "Enter your search string!" ' Set prompt.
    Title = "Find Card Number On all sheets!" ' Set title.
    Default = "" ' Set default.
    ' Display message, title, and default value.
    SearchString = InputBox(Message, Title, Default)
    
    'SearchString = "Rob"
    Set S = ActiveSheet
    For Each S In ActiveSheet
    With S.Range("A2:A1001")
    Set F = .Find(SearchString, MatchCase:=True, LookAt:=xlWhole, LookIn:=xlValues)
    If F Is Nothing Then
    Else
    Location = F.Address
    S.Select
    Range(Location).Select
    Exit For
    End If
    End With
    Next S

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Why wont my search function work when i change it from all sheets to active sheet?

    remove the
    For Each S In ActiveSheet
    and the
    Next S
    lines
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Why wont my search function work when i change it from all sheets to active sheet?

    This will work, no need to loop

            With Range("A2:A1001")
                Set F = .Find(SearchString, MatchCase:=True, LookAt:=xlWhole, LookIn:=xlValues)
                If F Is Nothing Then
                
                Else
                    Location = F.Address
                    s.Select
                    Range(Location).Select
                Exit For
                End If
            End With
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  4. #4
    Forum Contributor
    Join Date
    06-25-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2003
    Posts
    179

    Re: Why wont my search function work when i change it from all sheets to active sheet?

    Thanks that worked!

  5. #5
    Forum Contributor stojko89's Avatar
    Join Date
    05-18-2009
    Location
    Maribor, Slovenia
    MS-Off Ver
    MS Office 365
    Posts
    913

    Re: Why wont my search function work when i change it from all sheets to active sheet?

    Try this.
    It worked for me and it found Rob on 3 sheets in column A
    Sub MySearch()
    Dim ws As Worksheet
    Dim s As Object
    Dim Message, Title, Default, SearchString
    Message = "Enter your search string!" ' Set prompt.
    Title = "Find Card Number On all sheets!" ' Set title.
    Default = "" ' Set default.
    ' Display message, title, and default value.
    SearchString = InputBox(Message, Title, Default)
    
    'SearchString = "Rob"
    For Each ws In Worksheets
    With ws.Range("A2:A1001")
    Set F = .Find(SearchString, MatchCase:=True, LookAt:=xlWhole, LookIn:=xlValues)
    If F Is Nothing Then
    MsgBox ("Not Found")
    Else
    Location = F.Address
    MsgBox Location
    End If
    End With
    Next
    End Sub

  6. #6
    Forum Contributor stojko89's Avatar
    Join Date
    05-18-2009
    Location
    Maribor, Slovenia
    MS-Off Ver
    MS Office 365
    Posts
    913

    Re: Why wont my search function work when i change it from all sheets to active sheet?

    Ahhh never mind they beat me to the punch hehe

  7. #7
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Why wont my search function work when i change it from all sheets to active sheet?

    @ joshnathan

    Based on your last post it seems that you are satisfied with the solution(s) you've received but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

+ 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