Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default 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!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default 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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default 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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 46
Default 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





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Replace - Replace with Blank Space Studebaker Excel Discussion (Misc queries) 4 April 3rd 23 10:55 AM
where to put results of find operation in find and replace functio DEP Excel Worksheet Functions 5 November 15th 06 07:52 PM
find and replace - replace data in rows to separated by commas msdker Excel Worksheet Functions 1 April 15th 06 01:00 AM
Using Find and Replace to replace " in a macro snail30152 Excel Programming 1 April 13th 06 11:58 PM
Replace method - cannot find any data to replace Mike Excel Programming 5 April 6th 06 08:56 PM


All times are GMT +1. The time now is 11:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"