Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find a specific value in a range and replace it with a new value
#1
Example:

Sub FindAndReplace()
    Dim searchRange As Range
    Dim findValue As Variant
    Dim replaceValue As Variant

    'Set the range where you want to find and replace values
    Set searchRange = Range("A1:A10")

    'Specify the value to find and the value to replace it with
    findValue = "OldValue"
    replaceValue = "NewValue"

    'Find and replace the value in the range
    searchRange.Replace What:=findValue, Replacement:=replaceValue, LookAt:=xlWhole
End Sub

Explanation:

The code defines a subroutine named FindAndReplace.
It declares variables for the search range (searchRange), the value to find (findValue), and the value to replace it with (replaceValue).
Set searchRange = Range("A1:A10") specifies the range A1:A10 where you want to find and replace values.
findValue = "OldValue" and replaceValue = "NewValue" set the values to be found and replaced, respectively.
The searchRange.Replace method is used to find and replace the value. 
What:=findValue specifies the value to find, Replacement:=replaceValue specifies the value to replace it with, and LookAt:=xlWhole indicates that it should find whole matches.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Format cells in a range with bold font and yellow background color Qomplainerz 0 767 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Protect specific cells with VBA Qomplainerz 0 374 07-27-2023, 10:39 AM
Last Post: Qomplainerz
  Sort data in a range with VBA Qomplainerz 0 362 07-27-2023, 09:43 AM
Last Post: Qomplainerz
  Find the maximum value in a range with VBA Qomplainerz 0 362 07-27-2023, 07:54 AM
Last Post: Qomplainerz
  Range methods in VBA Qomplainerz 0 302 07-27-2023, 06:29 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)