PDA

View Full Version : [SOLVED:] Find And Replace with Increment Number Excel VBA



Aju0652
01-27-2021, 09:41 PM
Hello All,

is there any Excel VBA code that should help me for find and replace? example i want to find "STD" in excel Sheet and then Replace it With "Division 1" after that next find "STD" and it would replace to "Division 2" and so on... for each STD find the replacement would be increment the Division Number Like "Division 3", "Division 4", "Division 5" and so on... until the last STD find. Please find the attachment for the refrence.

mancubus
01-28-2021, 12:52 AM
Sub vbax_68341_find_replace_increment_multiple_occurrences_v()

Dim LastCell As Range, FoundCell As Range
Dim FirstAddress As String
Dim Counter As Long

On Error Resume Next

With ActiveSheet.UsedRange
Set LastCell = .Cells(.Cells.Count)
Set FoundCell = .Find(What:="STD", After:=LastCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
If Not FoundCell Is Nothing Then
FirstAddress = FoundCell.Address
Counter = 1
FoundCell.Value = "Division " & Counter
Do
Set FoundCell = .FindNext(FoundCell)
Counter = Counter + 1
FoundCell.Value = "Division " & Counter
Loop Until FoundCell.Address = FirstAddress
End If
End With

End Sub

Aju0652
01-28-2021, 01:12 AM
Thank You So Much Bro. You Don't Know How Much you Help Me... Love You A Lot. :bow::bow::clap2::super::super:

snb
01-28-2021, 04:09 AM
Sub M_snb()
On error Resume Next
n = 1

Do
Cells.Find("STD") = "snb_" & n
n = n + 1
Loop Until Err.Number <> 0
End Sub