Macro to replace part of the text in a cell and move it to another cell.

samcd87

New Member
Joined
Apr 8, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
  2. Web
I have a macro which takes the text out of col B of the active row and adds some other text and moves it to another column (C)

Often the text in col B will start with "● Alert - 77777777 has done [something]".

"● Alert - " is constant but the number (in this case "77777777") will change depending on the active row. The number is drawn from col D of the active row.

Before the text in col B is moved to col C and more text is added, I'd like to remove ● Alert - 77777777. Is there any way to do this?

The code I currently have is:
Sheets("AddCap").Range("C" & (ActiveCell.Row)).Value = "History of property - removed because" & Sheets("AddCap").Range("B" & (ActiveCell.Row)).Value

But this results in col C saying:
History of property - removed because ● Alert - 77777777 has done something

And I'd like the result in col C to be:
History of property - removed because they have done something

And this is what is contained on each row in the sheet named AddCap:

ABCD
Irrelevant● Alert - 77777777 has done something77777777


Any help would be appreciated.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi there...

What about...

VBA Code:
Sheets("AddCap").Range("C" & (ActiveCell.Row)).Value = "History of property - removed because they have done something"

Screenshot 2022-08-17 130255.png
 
Upvote 0
Apologies I haven't been clear here - the 'has done something' element of the text will vary depending on the row, so this is the bit that needs to be pulled over to C.

So it could say:
● Alert - 77777777 has eaten lunch
● Alert - 77777777 has walked the dog
or any number of things - these are just random examples of course.

The result we are then looking for in col C is:
History of property - removed because they have eaten lunch
and
History of property - removed because they have walked the dog
 
Upvote 0
Try the below:

VBA Code:
Sub test()
    Dim var As String
    Dim output As String
        var = Range("B" & (ActiveCell.Row)).Value
            output = Right(var, Len(var) - 18)
                Sheets("AddCap").Range("C" & (ActiveCell.Row)).Value = "History of property - removed because they " & output & " "
End Sub

Screenshot 2022-08-17 140628.png
 
Upvote 0
try this

VBA Code:
Sub Do_It()
    Dim sh As Worksheet
    Dim rng As Range, c As Range
    Dim s As String, d As Integer, t As String

    Set sh = ActiveSheet
    s = "History of property - removed because they "
    
    With sh
        Set rng = .Range("B1:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
        
        For Each c In rng.Cells
            d = InStr(c, "has") + 3
            If d > 3 Then
                t = Mid(c, d, Len(c))
                t = "have" & t
                c.Offset(, 1) = s & t
            End If
        Next
        
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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