VBA using input boxes to find and replace

Access Beginner

Active Member
Joined
Nov 8, 2010
Messages
311
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

I recorded a macro for a find and replace and was hoping if someone could tweak so that the "What" is and input box and the "Replacement" is also an input box


Code:
Sub Find_and_Replace()
'
' Find_and_Replace Macro
'
'
    Sheets("Data Table3").Select
    Range("D3:D18").Select
    Selection.Replace What:="20131227", Replacement:="20140627", LookAt:= _
        xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this:

Code:
Sub Find_and_Replace()




    Dim Title As Variant
    Dim sInput(2) As Variant
    Dim i As Integer


    Title = Array("Search", "Replace")
    i = 0
    Do
        sInput(i) = InputBox("What is the " & Title(i) & " Value?", Title(i))
        If StrPtr(sInput(i)) = 0 Then Exit Sub
        If Len(sInput(i)) = 0 Then
            MsgBox "You Must Enter a " & Title(i) & " Value.", 48, "Entry Required"


        Else
            i = i + 1
        End If
    Loop Until i > 1


    Sheets("Data Table3").Range("D3:D18").Replace What:=sInput(0), _
                                                  Replacement:=sInput(1), _
                                                  LookAt:=xlPart, _
                                                  MatchCase:=False
End Sub

Dave
 
Upvote 0
Hello!
This worked for me! I need to add in a button on the sheet to make this input box come up and add a message after the replace is done. Any ideas on how to do this?
 
Upvote 0

Forum statistics

Threads
1,215,309
Messages
6,124,180
Members
449,146
Latest member
el_gazar

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