+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    multiple returns from find function

    Hi there,

    I have a find function I am using with the below macro. It pastes the result into the specified cell but I am wondering...if the search query has more than one result then how do I do this and then paste it into the destination cell?

    I have pasted the code below which finds the first value it finds and pastes it into my destination cell.

    Code:
    Sub sortlinkups()
    
    Dim sourceSheet, destSheet As Worksheet
    Dim n As Long
    Dim units As String
    Dim foundString As Range
    Dim destString As String
    Dim foundVar As Variant
    
    Set sourceSheet = Sheets("Sheet1")
    Set destSheet = Sheets("LB03 CMS Link-Up References")
     
    sourceSheet.Activate
    
    For n = 54 To 186
    
        units = Cells(n, 5).Value
        
        With destSheet
            Set foundString = .Cells.Find(What:=units, After:=ActiveCell, LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
                
        If Not foundString Is Nothing Then
            sourceSheet.Cells(n, 7) = foundString.Offset(0, -1).Value + ", " + sourceSheet.Cells(n, 7).Value
        Else
            Set foundString = .Cells.Find(What:=split_text(units), After:=ActiveCell, LookIn:=xlValues, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
                
            If Not foundString Is Nothing Then
            sourceSheet.Cells(n, 7) = foundString.Offset(0, -1).Value + ", " + sourceSheet.Cells(n, 7).Value
            End If
        End If
        End With
    Next n
    
    End Sub
    I have also pasted the swap string function I am using just for reference:

    Code:
    Function split_text(data As String) As String
    Dim swapped As Variant
    
       swapped = Split(data, "-")
       split_text = swapped(1) & "-" & swapped(0)
    
    End Function
    I hoped there was an easy way to do this but I couldn't find one. If anyone knows then that would really help.

    Thanks,
    Jag

  2. #2
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: multiple returns from find function

    What do you want to do with the additional find results? (i.e. where do you want to put them?)
    So long, and thanks for all the fish.

  3. #3
    Registered User
    Join Date
    09-08-2009
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 SP3
    Posts
    45

    Re: multiple returns from find function

    Hey there, I am looking to add them to the destination cell. So basically concatenate the value with the new value if another is found, example would be:

    Code:
    sourceSheet.Cells(n, 7) = "the contents of the cell "+ " "+ "the next result returned by the find function"
    Jag

  4. #4
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: multiple returns from find function

    Something like this then, I think:
    Code:
    Sub sortlinkups()
    
    Dim sourceSheet, destSheet As Worksheet
    Dim n As Long
    Dim units As String
    Dim foundString As Range, strAddy As String
    Dim destString As String
    Dim foundVar As Variant
    
    Set sourceSheet = Sheets("Sheet1")
    Set destSheet = Sheets("LB03 CMS Link-Up References")
     
    With sourceSheet
    
        For n = 54 To 186
        
            units = .Cells(n, 5).Value
            
            With destSheet
                Set foundString = .Cells.Find(What:=units, After:=ActiveCell, LookIn:=xlValues, _
                    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=False, SearchFormat:=False)
                    
                If Not foundString Is Nothing Then
                    strAddy = foundString.Address
                    Do
                        sourceSheet.Cells(n, 7) = foundString.Offset(0, -1).Value & ", " & sourceSheet.Cells(n, 7).Value
                        Set foundString = .Cells.FindNext(foundString)
                    Loop While foundString.Address <> strAddy
                Else
                    Set foundString = .Cells.Find(What:=split_text(units), After:=ActiveCell, LookIn:=xlValues, _
                        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False)
                        
                    If Not foundString Is Nothing Then
                        strAddy = foundString.Address
                        Do
                            sourceSheet.Cells(n, 7) = foundString.Offset(0, -1).Value & ", " & sourceSheet.Cells(n, 7).Value
                            Set foundString = .Cells.FindNext(foundString)
                        Loop While foundString.Address <> strAddy
                    End If
                End If
            End With
        Next n
    End With
    End Sub
    So long, and thanks for all the fish.

Thread Information

Users Browsing this Thread

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

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.2.0