VBA Find/Replace but have input box for replacement value

Knockoutpie

Board Regular
Joined
Sep 10, 2018
Messages
116
Office Version
  1. 365
Platform
  1. Windows
Hi all,

is there a way to have the below code prompt for user input for the replacement value?

The below is basically searching the entire sheet for "$1" and replacing it with "$2"

But I was wondering if there was a way to search "$numerical value" and replace with "$inputboxvalue"
VBA Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Cells.Replace What:="$1", Replacement:="$2", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=True, _
        FormulaVersion:=xlReplaceFormula2
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
maybe
VBA Code:
Sub Macro1()
Dim strInput As String

strInput = InputBox("Enter replacement value")
If strInput <> "" Then
    Cells.Replace What:="$1", Replacement:=strInput, LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=True, _
        FormulaVersion:=xlReplaceFormula2
End If

End Sub
 
Upvote 0
Solution
maybe
VBA Code:
Sub Macro1()
Dim strInput As String

strInput = InputBox("Enter replacement value")
If strInput <> "" Then
    Cells.Replace What:="$1", Replacement:=strInput, LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=True, _
        FormulaVersion:=xlReplaceFormula2
End If

End Sub
Thank you! Do you know if there’s a way to search for “$numerical value” instead of specifying “$1”?
 
Upvote 0
If I understand the question, then basically the same method. strInput is a reference to a value provided by an input box. All you should need is a reference to the numerical value, but I have no idea what that is in your situation. Is that something that is in a cell on a sheet?
 
Upvote 0
If I understand the question, then basically the same method. strInput is a reference to a value provided by an input box. All you should need is a reference to the numerical value, but I have no idea what that is in your situation. Is that something that is in a cell on a sheet?
I ended up putting the value into a cell and having the cell be refrenced in the "find", so when i replace the value on the whole sheet it also replaces the cell value.
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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