Find & Replace unknown values

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
Hi, can anyone help, i'm trying to get sheet 2 (Slave Fund Numbers column A to replace all numbers found from Sheet 1 column D with their replacing numbers Sheet 1 column E

e.g. D2 is 1, E2 is 3, Sheet 2 Column A has 30 values of 1 = replace those 30 1's with new number 3. The code below only changes the first one found and not all of them? Help!!!


Private Sub CommandButton2_Click()
Dim i As Long
Dim LastRow As Long
Dim sh As Worksheet
Dim cell As Range

Set sh = Worksheets("Slave Fund Numbers")

With ActiveSheet

LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
For i = 2 To LastRow

Set cell = Nothing
On Error Resume Next
Set cell = sh.Columns("A").Find(.Cells(i, "D").Value)
On Error GoTo 0
If Not cell Is Nothing Then cell.Value = .Cells(i, "E").Value
Next i
End With

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try...

Code:
Private Sub CommandButton2_Click()

    Dim i As Long
    Dim LastRow As Long
    Dim sh As Worksheet
    
    Set sh = Worksheets("Slave Fund Numbers")
    
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
        For i = 2 To LastRow
            sh.Columns("A").Replace what:=.Cells(i, "D"), replacement:=.Cells(i, "E"), _
                LookAt:=xlWhole, searchorder:=xlByColumns, MatchCase:=False
        Next i
    End With
    
End Sub
 
Upvote 0
Thanks Mate, works great. 1 small thing if Column E is blank, no value it replaces it finds D in column A and replaces this with blank.

Any way i can get this to ignore blank cells in column E?
 
Upvote 0
Thanks Mate, works great.

You're welcome!

Any way i can get this to ignore blank cells in column E?

Try...

Code:
Private Sub CommandButton2_Click()

    Dim i As Long
    Dim LastRow As Long
    Dim sh As Worksheet
    
    Set sh = Worksheets("Slave Fund Numbers")
    
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
        For i = 2 To LastRow
            If .Cells(i, "E").Value <> "" Then
                sh.Columns("A").Replace what:=.Cells(i, "D"), replacement:=.Cells(i, "E"), _
                    LookAt:=xlWhole, searchorder:=xlByColumns, MatchCase:=False
            End If
        Next i
    End With
    
End Sub
 
Upvote 0
That has worked perfectly, one more question while your helping me out, slightly different to the last.

I have this code...that moves data from 1 sheet to another putting it in the next empty row!

Private Sub CommandButton1_Click()
Sheets("Scenario Options").Range("Z1:Z10").Copy Destination:=Sheets("Scenario Log").Range("A500").End(xlUp).Offset(1, 0)
End Sub

But I'm having a little trouble trying to get this to copy and paste values as some of the cells have fomulas in and this ends up as #REF being copied?
 
Upvote 0
Try...

Code:
Private Sub CommandButton1_Click()
    Sheets("Scenario Options").Range("Z1:Z10").Copy
    Sheets("Scenario Log").Range("A500").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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