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.
I have also pasted the swap string function I am using just for reference: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 hoped there was an easy way to do this but I couldn't find one. If anyone knows then that would really help.Code:Function split_text(data As String) As String Dim swapped As Variant swapped = Split(data, "-") split_text = swapped(1) & "-" & swapped(0) End Function
Thanks,
Jag
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.
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:
JagCode:sourceSheet.Cells(n, 7) = "the contents of the cell "+ " "+ "the next result returned by the 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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks