How to use Find & Split & Replace/Join to revise part of specific string?

Macaron

New Member
Joined
Nov 13, 2021
Messages
24
Office Version
  1. 2019
Platform
  1. Windows
Hi,

I am trying to revise part of string in excel,
but my code cannot produce my expectation.
Hope somebody can help.

VBA Code:
Sub FindSplitReplace()
Dim sht As Worksheet
Dim wb As Workbook
Dim strBD As String
Dim arrSplitformat() As String
Dim rmkrng As Range
Dim Usedrng As Range
Dim Usedrmkrng As Range
Dim FirstAddr As String
Dim LastColumn As Long
Dim foundMCB As Range

MCBToFind = "MCB BD"

Set objNewWorkbook = Excel.Application.Workbooks.Add
Set objNewWorksheet = objNewWorkbook.Sheets(1)
Set wb = ThisWorkbook
Set wf = WorksheetFunction
For i = 1 To ThisWorkbook.Sheets.Count
    
    Set sht = wb.Sheets(i)
    Set Usedrng = sht.UsedRange
Set rmkrng = Usedrng.Cells.Find(What:="Remark", LookIn:=xlFormulas, LookAt _
        :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        True, MatchByte:=False, SearchFormat:=False)
              LastColumn = rmkrng.Column

Set Usedrmkrng = sht.UsedRange.Columns(LastColumn).Resize(, 2)

Set foundMCB = Usedrmkrng.Cells.Find(What:=MCBToFind, After:=rmkrng, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False)
        
    If Not foundMCB Is Nothing Then
        FirstAddr = foundMCB.Address
    End If
   
Do Until foundMCB Is Nothing
If Not foundMCB.Value Like "*TO '*" Then
arrSplitformat = Split(foundMCB.Value, " ")
strBD = arrSplitformat(0)
sht.Range(sht.Cells(foundMCB.Row), sht.Cells(foundMCB.Column)).Replace _
What:=strBD, Replacement:="TO ' & strBD & '", _
SearchOrder:=xlByColumns, MatchCase:=True
End If
Set foundMCB = Usedrmkrng.FindNext(After:=foundMCB)

        
        If foundMCB Is Nothing Then Exit Do
        If foundMCB.Address = FirstAddr Then Exit Do
    Loop
'-----------------------------------------------------
Next i
End Sub

Here is a part of my VBA code.
I would like to revise the first word that belongs to MCB BD. For Example, string "PA TPN MCB BD" changes to string "TO 'PA' TPN MCB BD", string "3TA SPN MCB BD" changes to string "TO '3TA' TPN MCB BD ,and so on. That means add "TO ' & strBD &'" to the first word. I have tried to use Replace and Join Function. But failed.


test test format1.xlsx
ABCDEFGHIJKLMNOPQRSTU
9
10
11Remark
12
13PA TPN MCB BD
14
15
163TA SPN MCB BD
17SPACE
18SPACE
192PB TPN MCB BD
20
21
22SPACE
233LA SPN MCB BD
24SPACE
253C TPN MCB BD
26
27
287B TPN MCB BD
29
30
31PDC TPN MCB BD
32
33
343LC TPN MCB BD
35
36
S3 (2)
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I may be misunderstanding your problem but would this work for you?

VBA Code:
Sub findAndPrepend()
    Dim sht As Worksheet, r As Range, s$, addr$
   
    s = "MCB BD"
    For Each sht In ThisWorkbook.Sheets
        Set r = sht.Cells.Find(s, sht.Range("A1"), xlValues, xlPart, xlByRows, xlNext, False)
        If Not r Is Nothing Then addr = r.Address
        While Not r Is Nothing
            If StrComp(Left(r.Value, 2), "TO", vbTextCompare) <> 0 Then r.Value = "TO '" & r.Value & "'"
            Set r = sht.Cells.FindNext(r)
            If r.Address = addr Then Set r = Nothing
        Wend
    Next sht
End Sub
 
Upvote 0
I may be misunderstanding your problem but would this work for you?

VBA Code:
Sub findAndPrepend()
    Dim sht As Worksheet, r As Range, s$, addr$
  
    s = "MCB BD"
    For Each sht In ThisWorkbook.Sheets
        Set r = sht.Cells.Find(s, sht.Range("A1"), xlValues, xlPart, xlByRows, xlNext, False)
        If Not r Is Nothing Then addr = r.Address
        While Not r Is Nothing
            If StrComp(Left(r.Value, 2), "TO", vbTextCompare) <> 0 Then r.Value = "TO '" & r.Value & "'"
            Set r = sht.Cells.FindNext(r)
            If r.Address = addr Then Set r = Nothing
        Wend
    Next sht
End Sub
almost correct.

I want ' add to the right and left of first word such as TO 'PA' TPN MCB BD,
not the whole value, so I have to use split function to revise before.
 
Upvote 0
Gotcha! :)

VBA Code:
Sub findAndPrepend()
    Dim sht As Worksheet, r As Range, s$, addr$
  
    s = "MCB BD"
    For Each sht In ThisWorkbook.Sheets
        Set r = sht.Cells.Find(s, sht.Range("A1"), xlValues, xlPart, xlByRows, xlNext, False)
        If Not r Is Nothing Then addr = r.Address
        While Not r Is Nothing
            If StrComp(Left(r.Value, 2), "TO", vbTextCompare) <> 0 Then r.Value = "TO '" & Replace(r.Value, " ", "' ", Count:=1)
            Set r = sht.Cells.FindNext(r)
            If r.Address = addr Then Set r = Nothing
        Wend
    Next sht
End Sub
 
Upvote 0
Solution
Gotcha! :)

VBA Code:
Sub findAndPrepend()
    Dim sht As Worksheet, r As Range, s$, addr$
 
    s = "MCB BD"
    For Each sht In ThisWorkbook.Sheets
        Set r = sht.Cells.Find(s, sht.Range("A1"), xlValues, xlPart, xlByRows, xlNext, False)
        If Not r Is Nothing Then addr = r.Address
        While Not r Is Nothing
            If StrComp(Left(r.Value, 2), "TO", vbTextCompare) <> 0 Then r.Value = "TO '" & Replace(r.Value, " ", "' ", Count:=1)
            Set r = sht.Cells.FindNext(r)
            If r.Address = addr Then Set r = Nothing
        Wend
    Next sht
End Sub
It works! Could you explain more about these code?
I don't understand ...
1. why add $ to s and addr.?
2. Does sht.Range("A1") for finding value in merged area?
3. what does Count:=1 mean?
 
Upvote 0
It works! Could you explain more about these code?
I don't understand ...
1. why add $ to s and addr.?
2. Does sht.Range("A1") for finding value in merged area?
3. what does Count:=1 mean?

1. Me being lazy. It's the type-decleration character for a string:

1645967125787.png
2. Yes :)

3. It's an argument for the replace function. By default it replaces every instance of the character searched for. Setting the count parameter to 1 just means that it will only replace the first instance found. a 2 would have replaced the first 2 instances, and so on and so on.


If you're interested at all in the type characters, here's the ones i know about:

Type characterData typeExample
%IntegerDim int%
&LongDim lng&
@DecimalDim dec@
!SingleDim sgl!
#DoubleDim dbl#
$StringDim str$
 
Last edited:
Upvote 0
1. Me being lazy. It's the type-decleration character for a string:

View attachment 58842
2. Yes :)

3. It's an argument for the replace function. By default it replaces every instance of the character searched for. Setting the count parameter to 1 just means that it will only replace the first instance found. a 2 would have replaced the first 2 instances, and so on and so on.


If you're interested at all in the type characters, here's the ones i know about:

Type characterData typeExample
%IntegerDim int%
&LongDim lng&
@DecimalDim dec@
!SingleDim sgl!
#DoubleDim dbl#
$StringDim str$
Oh I see!! Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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