0

I am trying to write a code that will search multiple ranges and match the first half of a string. if the match is true it would them replace the cell in the range with the cell in the column.

I found this code and made some changes to search multiple columns in multiple ranges on different sheets and simply replace if the first part of the string matches.

The other problem I have is that I need it to search part of the string in the cell for example

In the range; 879841.42859-MD_42885 From the column; 879841.42859-MD_43

I want it to match 879841.42859-MD then replace 879841.42859-MD_43885 with 879841.42859-MD_43

' Matchandreplace1 Macro

' Code from stack overflow cut down, no sheets involved. '

Dim ShSrc As Worksheet, ShTar As Worksheet
Dim SrcLRow As Long, TarLRow As Long, NextEmptyRow As Long
Dim RefList As Range, TarList As Range, RefCell As Range, RefColC
Dim TarCell As Range, TarColC As Range
Dim IsFound As Boolean
Dim ToFind As String

With ThisWorkbook
    Set ShSrc = .Sheets("Sheet1")
    Set ShTar1 = .Sheets("Sheet2")
    Set ShTar2 = .Sheets("Sheet3")
End With

'Get the last rows for each sheet.
SrcLRow = ShSrc.Range("A" & Rows.Count).End(xlUp).Row
TarLRow = ShTar1.Range("A" & Rows.Count).End(xlUp).Row
TarLRow = ShTar2.Range("A" & Rows.Count).End(xlUp).Row

'Set the lists to compare.
Set RefList = ShSrc.Range("A2:A" & SrcLRow)
Set TarList = ShTar1.Range("A2:A" & TarLRow)
Set TarList = ShTar2.Range("A2:A" & TarLRow)

'Initialize boolean, just for kicks.
IsFound = False

'Speed up the process.
Application.ScreenUpdating = False

'Create the loop.
For Each RefCell In RefList

    ToFind = RefCell.Value

    'Look for the value in our target column.
    On Error Resume Next
    Set TarCell = TarList.Find(ToFind)
    If Not TarCell Is Nothing Then IsFound = True
    On Error GoTo 0

    'If value exists in target column...
    If IsFound Then
        'set the value to match and highlight.
            TarColC.Value = RefColC.Value
            TarColC.Interior.ColorIndex = 4
        End If

    'Set boolean check to False.
    IsFound = False

Next RefCell
Application.ScreenUpdating = True

End Sub

Thanks,

Jerome A few snips to better describe

JRR
  • 25
  • 5
  • What are all the ranges 879841.42859-R1_43224 etc? What precisely isn't working? Can you show some sample data and desired output in an image? – QHarr Oct 14 '17 at 15:22
  • You may end up wanting to loop each cell in the range and use InStr to find if the cell contents have a match for 879841.42859-R1. You can combine that with the SUBSTITUTE function change 879841.42859-R1_43224 for 879841.42859-R1_42554. Is there any reason why you can't just use SUBSTITUTE 879841.42859-R1_43224 for 879841.42859-R1_42554 on a range direct without a test for the string 879841.42859-R1? – QHarr Oct 14 '17 at 15:27
  • Hi, I added some images to better explain. – JRR Oct 14 '17 at 16:09
  • I don't see a reason why I couldn't use substitute, – JRR Oct 14 '17 at 16:15
  • None of the values shown appear to match what you stated in the question i.e. 879841.42859-R1. Is this a string (value) you are looking for? What is the purpose of sheet 1 and why are the highlighted yellow ones replaced in sheets 2 and 3? I think your question needs an edit so i can, for one, can try to help. Please see [Minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) and [How to ask a good question](https://stackoverflow.com/help/how-to-ask). SO is here to help if you can help us. – QHarr Oct 14 '17 at 16:17
  • Example use of substitute https://stackoverflow.com/questions/17561274/substitute-function-in-excel-vba-for-cell-range – QHarr Oct 14 '17 at 16:19
  • Ok, I will have upwards of 20 ranges, on 20 different sheets is there an easy way to code it to loop through all the ranges? – JRR Oct 14 '17 at 16:33
  • Rather Than using Substitute and looping through, We can use the find and replace method . Just add * before and after the find string , So when a partial match for the cell is found, the whole cell is replaced. Check out my answer below – Valli Oct 14 '17 at 16:35
  • Very true though i think we still need to establish the rules of engagement on this question. – QHarr Oct 14 '17 at 16:37

1 Answers1

0

The code you have pasted has lot of syntax errors. Excel has got find and replace function which can replace the string it has found. Your requirement is to find a portion of a cell ( from source) and replace the whole cell in target

You have to append a * before and after the find string and it will replace the whole cell. Am assuming that you need to match first 15 letters of the "Find" String

    Sub FindReplaceAll1()
    Dim sht As Worksheet
    Dim fnd As Variant
    Dim rplc As Variant
    Dim Found As Range
    Dim Checkcol As Integer, rowcount As Integer, TcurrentRow As Integer, currentRow As Integer, Targrowcount As Integer
    Checkcol = 1 'Denotes A column

     Sheets("Sheet1").Select
    rowcount = Cells(Rows.Count, Checkcol).End(xlUp).Row
    For currentRow = 1 To rowcount
'Find the substring for which you need to match. Am taking first 15 characters.
    fnd = Left$(Cells(currentRow, Checkcol).Value, 15)
    rplc = Cells(currentRow, Checkcol).Value

    For Each sht In ActiveWorkbook.Worksheets
    If sht.Name = "Sheet2" Or sht.Name = "Sheet3" Then
    'Replace the whole string when a partial match is achieved
     sht.Cells.Replace what:="*" & fnd & "*", Replacement:=rplc, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
    End If
    Next sht
    Next
    End Sub
Valli
  • 1,440
  • 1
  • 8
  • 13
  • Hello Valli, If i would like it to match everything on the Left side of the "_" would I be able to use a find and LEN type of approach? – JRR Oct 14 '17 at 16:44
  • Yes. Use instr to locate "_". So your line fnd = Left$(Cells(currentRow, Checkcol).Value, 15) should be changed to fnd = Left$(Cells(currentRow, Checkcol).Value, InStr(Cells(currentRow, Checkcol).Value, "_")) – Valli Oct 14 '17 at 16:51
  • 1
    Thanks for your help Valli – JRR Oct 14 '17 at 16:56
  • One last concern if I want to limit the range on the sheet say from A16:N46 how would I do that? – JRR Oct 14 '17 at 17:00
  • Which sheet? Source? – Valli Oct 14 '17 at 17:01
  • No I am just wondering how, if i need to in the future, I could quickly manipulate the searched area on the Sheet2 and Sheet3 – JRR Oct 14 '17 at 17:10
  • The cells I am searching and replacing are formulas, would I have to put something like SpecialCells(xlCellTypeFormulas, xlTextValues) in the Fnd instr? – JRR Oct 15 '17 at 17:23
  • I dont get you.. Are you replacing the formula of a cell or the value of cells that have formulas? – Valli Oct 15 '17 at 17:40
  • The data in the 2nd and 3rd sheets are formulas, I think that is why nothing is getting replaced when I run the code. I want to replace it with a Value(sheet1), so it would have to find what the text in the cell is and not look at the code. does that make sense? – JRR Oct 15 '17 at 18:57
  • yes, it will not work with formulas. You have to copy the cells with formulas and paste only values. Then apply the macro – Valli Oct 15 '17 at 19:43
  • so there is no way to get it to look at the text value in the cell? copying all the data and pasting only values would be an extra step id like to avoid – JRR Oct 16 '17 at 08:45