+ Reply to Thread
Results 1 to 8 of 8

Search query help required

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Search query help required

    =PROBLEM SOLVED, thank you DonkeyOte=

    Hi there,

    I need a macro that can search through all of my worksheets bar the first one looking for the string given by the value of cell (sheet1,A,2). If it finds the string in sheet2 it should fill cell (sheet1,B,2) with the string "True". If found in sheet3, it should fill cell (sheet1,C,2) with "True", etc.

    Once it has done this, it should be able to loop and do the same for the string given in (sheet1,A,3), etc.

    Looking back on what I've just written I'm not sure how well I've explained myself, so I'll try to convey what I want done in pseudocode:

    For each cell (X) in sheet1,column1
            For each worksheet (Y+1) [discounting sheet1]
                     Seach for cellX.Value in sheetY+1
                               If found, sheet1,rowX,columnY+1 = "True"
            Next Y
    Next X
    Hopefully that'll explain what I want done and hopefully one of you macro-geniuses will be able to help me!

    Cheers!
    Last edited by Jimple Fish; 06-30-2009 at 08:38 AM. Reason: Problem solved - thanks to DonkeyOte

  2. #2
    Registered User
    Join Date
    07-03-2008
    Location
    Hyderabad, India
    MS-Off Ver
    2003 and 2007
    Posts
    58

    Thumbs up Re: Search query help required

    Hi Try this,

    Option Explicit
    
    Sub Test()
    Dim wsMain As Excel.Worksheet
    Dim wsOther As Excel.Worksheet
    
    Dim searchRange As Excel.Range
    Dim cl As Excel.Range
    
    ' Set main page
    Set wsMain = ThisWorkbook.Sheets("Sheet1")
    Set searchRange = wsMain.Range("A:A")
    
    For Each wsOther In ThisWorkbook.Sheets
        If Not (wsOther.Name = wsMain.Name) Then
            For Each cl In searchRange
                If GetSeachTextExist(wsOther, cl.Value) Then
                    wsMain.Cells(cl.Row, cl.Column + 1) = "True"
                End If
            Next
        End If
    Next
    End Sub
    
    ' Function to search the text
    Private Function GetSeachTextExist(wsSearch As Excel.Worksheet, searchText As String) As Boolean
        Dim SearchResult As Excel.Range
        GetSeachTextExist = False
     
        With wsSearch
            Set SearchResult = .Cells.Find(What:=searchText, after:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
            
            If Not SearchResult Is Nothing Then
                GetSeachTextExist = True
            End If
        End With
    End Function
    Regards,
    Salim
    Last edited by salimudheen; 06-26-2009 at 10:01 AM.
    Salim

  3. #3
    Registered User
    Join Date
    06-26-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Search query help required

    Hi Salim, thanks for your prompt reply!

    Tried using your code and I got an overflow error on the line:

    Set SearchResult = .Cells.Find(What:=searchText, after:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
    Any idea how to solve this?

    Thanks for helping!

  4. #4
    Registered User
    Join Date
    07-03-2008
    Location
    Hyderabad, India
    MS-Off Ver
    2003 and 2007
    Posts
    58

    Thumbs up Re: Search query help required

    Hi,

    I make some changes in code.

    Below is the code,

    Option Explicit
    
    Sub Test()
    Dim wsMain As Excel.Worksheet
    Dim wsOther As Excel.Worksheet
    
    Dim searchRange As Excel.Range
    Dim cl As Excel.Range
    
    ' Set main page
    Set wsMain = ThisWorkbook.Sheets("Sheet1")
    Set searchRange = wsMain.Range("A:A")
    
    ' Change this set of code
    For Each cl In searchRange
        If (cl.Value <> "") Then
            For Each wsOther In ThisWorkbook.Sheets
                If Not (wsOther.Name = wsMain.Name) Then
                    If GetSeachTextExist(wsOther, cl.Value) Then
                        wsMain.Cells(cl.Row, cl.Column + 1) = "True"
                        Exit For
                    End If
                End If
            Next
        Else
            Exit For
        End If
    Next
    
    End Sub
    
    ' Function to search the text
    Private Function GetSeachTextExist(wsSearch As Excel.Worksheet, searchText As String) As Boolean
        Dim SearchResult As Excel.Range
        GetSeachTextExist = False
     
        With wsSearch
            Set SearchResult = .Cells.Find(What:=searchText, after:=.Cells(.Cells.Count), LookIn:=xlValues, lookat:=xlWhole, SearchOrder:=xlByRows, searchdirection:=xlNext, MatchCase:=False)
            
            If Not SearchResult Is Nothing Then
                GetSeachTextExist = True
            End If
        End With
    End Function
    Regards,
    Salim

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Search query help required

    Here is another variant on the same theme....

    Public Sub Example()
    Dim wsMain As Worksheet, wsOther  As Worksheet, rngCell As Range, bFound As Boolean
    For Each rngCell In ActiveSheet.Range(Cells(2, "A"), Cells(Rows.Count, "A").End(xlUp)).Cells
        If LenB(rngCell.Value) Then
            For Each wsOther In ThisWorkbook.Sheets
                If Not wsOther.Name = ActiveSheet.Name Then
                    With wsOther: bFound = Not .Cells.Find(rngCell.Value, lookat:=xlWhole) Is Nothing: End With
                    If bFound Then rngCell.Offset(, 1).Value = CStr(bFound): Exit For
                End If
            Next wsOther
        End If
    Next rngCell
    End Sub

  6. #6
    Registered User
    Join Date
    06-26-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Search query help required

    DonkeyOte, your code appears to be almost perfect - the only problem is that when it fills the relevant cells in with "TRUE", it only does so in the second column. I need it to fill in the cell where the column matches up to the sheet it was found in: EG, if it found the text in sheet3, it says "TRUE" in column 3. If found in sheet2 and sheet4, it says "TRUE" in columns 2 and 4.

    Thanks for this. (If anyone else knows how to help, please feel free to chip in!)

  7. #7
    Registered User
    Join Date
    06-26-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Search query help required

    I worked it out - thank you so much for your help DonkeyOte! Couldn't have done this without you.

+ 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