VBA Find and Replace based on a specific cell

evenvasveen

New Member
Joined
Jun 13, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

I want to use macros to find and replace a text based on a specific cell value.
For example: if the text is "001563 0105" and i want to use macros to find "001563" and replace it with "", so in the end it just show "0105".

If cell value A1 contain the text "001563", the codes find this in the text "001563 0105" and replace it with "0105".
If i change cell value A1 with for example "001560", the codes find this text and remove it.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the Board!

The issue with trying to use Find/Replace (either manually or by VBA) is that it will when you drop the first number it will convert the second portion to a number, and you will lose the leading zero (so it would show "105", not "0105"). So it might be better to loop through the range you want to do the replacements in.
Can you tell me what range this VBA code should be searching in to make these replacements?
 
Upvote 0
Okey, I see. Technically it doesn’t matter if I loose the leading zero. It will still work with or without the zero in front (the four numbers stands for a date, like in my example 0105 stands for May 1th).

My range of where the VBA code should be searching for is H7:H100.
Thank you!
 
Upvote 0
OK, then this should be fairly easy. Try this:
VBA Code:
Sub MyReplaceMacro()

    Dim str As String
    
'   Get value to replace from cell A1 (make sure blank space at end)
    str = Trim(Range("A1")) & " "

'   Replace values in range
    Range("H7:H100").Replace What:=str, Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
        
End Sub
 
Upvote 0
Solution
OK, then this should be fairly easy. Try this:
VBA Code:
Sub MyReplaceMacro()

    Dim str As String
   
'   Get value to replace from cell A1 (make sure blank space at end)
    str = Trim(Range("A1")) & " "

'   Replace values in range
    Range("H7:H100").Replace What:=str, Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
       
End Sub
Thank you so much! It worked perfectly.
 
Upvote 0
You are welcome!
Glad I was able to help.
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,664
Members
449,045
Latest member
Marcus05

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