Find & Replace unknown values

MichaelRSnow

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

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,244
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
379
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
19,244
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
379

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
19,244
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,590
Messages
5,512,257
Members
408,886
Latest member
kashifziatevta423

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top