vba change cell content

ottasight

New Member
Joined
Feb 15, 2009
Messages
47
Hi, i am trying to find certain strings in a column list that contains construction tasks. once found, the cell is rewritten with only the search string. for example, variable X is "dig footers". the cell contains, "start foundation, dig footers, (po435) thursday". i need the cell to only contain "dig footers". i'll be putting this in a loop to search the whole column, but what i have so far isn't working. thanks for any help
Sub Changecontent()
Dim xFind As String
DIM XREP AS STRING
XREP = "dig footers"
XFIND=ACTIVECELL
ACTIVECELL.SELECT
ACTIVECELL.Replace What:="*XFIND*", Replacement:=XREP, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
No need for any loop. Replace can do a whole range at once.

Here is some code that will do the whole selected area at once. You can set "rng" equal to any range (i.e. a whole column):
VBA Code:
Sub Changecontent()

    Dim XREP As String
    Dim rng As Range
   
'   Set range to search/replace
    Set rng = Selection

'   Designate value to find/replace
    XREP = "dig footers"

'   Do replacement on whole range at once
    rng.Replace What:="*" & XREP & "*", Replacement:=XREP, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
       
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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