VBA to replace redacted with full number

wwalters

New Member
Joined
Dec 12, 2018
Messages
5
On Sheet 1, I have a list of information, with Column B containing redacted numbers. The underacted numbers are in Column B on Sheet 2.

The two sheets will not be in the same order. I need help forming the code to replace redacted in sheet 1 with redacted in sheet 2. To make a match, the last 4 will numbers will match between the two and Column A will match as well. I think I could get by just matching the last 4 (I do not believe there are any duplicates, but would love a second verify that A matches).

I am lost. I have tried several things, none of which have worked.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Post the code you have and we can help you.

You can get the last 4 characters by using the RIGHT function.

You can use the FIND function to search:
Code:
    Dim Rng As Range
    Dim FoundRow As Long
    With ThisWorkbook.Worksheets(<sheetnamehere>).Range("B:B")[INDENT]Set Rng = .Find(What:=<searchstringhere>, _
</searchstringhere>[/INDENT]
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
<searchstringhere>         If Not Rng Is Nothing Then
</searchstringhere>[INDENT]   FoundRow = Rng.Row
Else
   'not found[/INDENT]
<searchstringhere>         End If
</searchstringhere>
</sheetnamehere><sheetnamehere><searchstringhere>     End With</searchstringhere></sheetnamehere>
 
Upvote 0
With Worksheets("Sheet2")
fullno = Range(.Cells(1, 1), .Cells(80, 1))
End With
With Worksheets("sheet1")
Redacted = Range(.Cells(1, 15), .Cells(80, 15))
For i = 1 To 80
num = Right(Redacted(i, 1), 2)
For j = 1 To 80
If num = Right(fullno(j, 1), 2) Then
Redacted(i, 1) = fullno(j, 1)
Exit For
End If
Next j
Next i
Range(.Cells(1, 15), .Cells(80, 15)) = Redacted
End With
 
Upvote 0
Code:
Sub Replace()


'assumes each sheet has 80 lines.
For InRow = 1 To 80
    InA = Worksheets("SheetA").Cells(InRow, 1)
    Redacted = Left(Worksheets("SheetA").Cells(InRow, 2), 4)
    
    For FullRow = 1 To 80
        'match column A
        If InA = Worksheets("SheetB").Cells(FullRow, 1) Then
            'match column B (partial)
            If Redacted = Left(Worksheets("SheetB").Cells(FullRow, 2), 4) Then
                'we have a match
                Worksheets("SheetA").Cells(InRow, 2) = Worksheets("SheetB").Cells(FullRow, 2)
                GoTo NextInRow
            End If
        End If
    Next FullRow
    
    'if you need to do something when there is no match, put that code here


NextInRow:


Next InRow


End Sub
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,645
Members
449,461
Latest member
kokoanutt

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top