ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Find/Replace from Table (https://www.excelbanter.com/excel-programming/361178-find-replace-table.html)

Steph

Find/Replace from Table
 
Hello. Is there a way to automate an Edit/Replace from a table? In column
A I have the values of what to Find, and in column B I have the Replace with
values. I know a workaround is to insert a column and do a lookup, but I
was hoping to dtreamline the process a little. Thanks!



Barry-Jon

Find/Replace from Table
 
This works - you can tweak it as meets your own needs. Please let me
know if this is the kind of thing you were looking for.

Sub MultiFindReplace()

Dim rngReplaceWith As Excel.Range
Dim rngSearchArea As Excel.Range
Dim lngRepaceCount As Long

Set rngReplaceWith = GetUserRange("Please select find/replace
values range (two columns)")

If Not rngReplaceWith Is Nothing Then

'basic range size validation - a lot more could be done
If rngReplaceWith.Columns.Count = 2 Then

'now get the area in which to do the find/replace
Set rngSearchArea = GetUserRange("Please select the range
in which to find/replace")

If Not rngSearchArea Is Nothing Then

'do the search and replace
For lngRepaceCount = 1 To rngReplaceWith.Rows.Count

rngSearchArea.Replace
What:=rngReplaceWith.Cells(lngRepaceCount, 1).Value, _

Replacement:=rngReplaceWith.Cells(lngRepaceCount, 2).Value, _
MatchCase:=False, _
ReplaceFormat:=False

Next lngRepaceCount

End If

Else

MsgBox "Invalid find/replace range selected", vbExclamation
+ vbOKOnly

End If

End If

End Sub

Private Function GetUserRange(Prompt As String, Optional Title As
String = "Input") As Excel.Range

On Error GoTo ErrorHandler

Dim retVal As Excel.Range

Set retVal = Application.InputBox(Prompt, Title, , , , , , 8)

ExitProc:
Set GetUserRange = retVal
Exit Function

ErrorHandler:
Set retVal = Nothing
Resume ExitProc

End Function


Steph

Find/Replace from Table
 
Thanks Barry-Jon,

When I copied the code in, I got a compile error on the following line:
rngSearchArea.ReplaceWhat:=rngReplaceWith.Cells(ln gRepaceCount, 1).Value, _
Replacement:=rngReplaceWith.Cells(lngRepaceCount, 2).Value,
MatchCase:=False, _
ReplaceFormat:=False

Any ideas? Thanks for your help!

"Barry-Jon" wrote in message
oups.com...
This works - you can tweak it as meets your own needs. Please let me
know if this is the kind of thing you were looking for.

Sub MultiFindReplace()

Dim rngReplaceWith As Excel.Range
Dim rngSearchArea As Excel.Range
Dim lngRepaceCount As Long

Set rngReplaceWith = GetUserRange("Please select find/replace
values range (two columns)")

If Not rngReplaceWith Is Nothing Then

'basic range size validation - a lot more could be done
If rngReplaceWith.Columns.Count = 2 Then

'now get the area in which to do the find/replace
Set rngSearchArea = GetUserRange("Please select the range
in which to find/replace")

If Not rngSearchArea Is Nothing Then

'do the search and replace
For lngRepaceCount = 1 To rngReplaceWith.Rows.Count

rngSearchArea.Replace
What:=rngReplaceWith.Cells(lngRepaceCount, 1).Value, _

Replacement:=rngReplaceWith.Cells(lngRepaceCount, 2).Value, _
MatchCase:=False, _
ReplaceFormat:=False

Next lngRepaceCount

End If

Else

MsgBox "Invalid find/replace range selected", vbExclamation
+ vbOKOnly

End If

End If

End Sub

Private Function GetUserRange(Prompt As String, Optional Title As
String = "Input") As Excel.Range

On Error GoTo ErrorHandler

Dim retVal As Excel.Range

Set retVal = Application.InputBox(Prompt, Title, , , , , , 8)

ExitProc:
Set GetUserRange = retVal
Exit Function

ErrorHandler:
Set retVal = Nothing
Resume ExitProc

End Function




Barry-Jon

Find/Replace from Table
 
Most likely spacing/line breaks from the copy / paste. Try putting the
code all on one line without the _ and line breaks.

rngSearchArea.Replace What:=rngReplaceWith.Cells(lngRepaceCount,
1).Value, Replacement:=rngReplaceWith.Cells(lngRepaceCount, 2).Value,
MatchCase:=False, ReplaceFormat:=False


Steph

Find/Replace from Table
 
That did it. Thanks! I'll give it a shot!!
"Barry-Jon" wrote in message
oups.com...
Most likely spacing/line breaks from the copy / paste. Try putting the
code all on one line without the _ and line breaks.

rngSearchArea.Replace What:=rngReplaceWith.Cells(lngRepaceCount,
1).Value, Replacement:=rngReplaceWith.Cells(lngRepaceCount, 2).Value,
MatchCase:=False, ReplaceFormat:=False





All times are GMT +1. The time now is 01:36 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com