Find & Replace unknown values

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
387
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,212
Office Version
  1. 365
Platform
  1. Windows
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
 

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
387
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?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,212
Office Version
  1. 365
Platform
  1. Windows
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
 

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
387

ADVERTISEMENT

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?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,212
Office Version
  1. 365
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,583
Messages
5,832,594
Members
430,144
Latest member
bruno92

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
Top