Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

excel macro query

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel Macro Query - Replace cell contents

Excel Macro Query - Replace cell contents

ResolvedVersion 2010

Adrian has attended:
Excel VBA Introduction course
Excel VBA Intermediate course
Excel Intermediate course

Excel Macro Query - Replace cell contents

Hi

I am struggling to create a macro that states if word SIETCO is found in cells within 1 column, then it automatically changes the corresponding cell contents in another column from EPTB TO EPTBSIET (ie B4, if SIETCO appeared in A4).

Any help would be greatly appreciated, many thanks

RE: Excel Macro Query - Replace cell contents

Hi Adrian,

Thank you for your post. You could use an IF function to change the appropriate cells according to your spec while leaving the rest unchanged.
Have a look at the sample macro I recorded below:

Sub Macro2()

'This macro runs on data in columns A and B of a worksheet.
Range("C1").Select
Range("C1") = "=IF(RC[-2]=""SIETCO"",""EPTBSIET"",RC[-1])"
'An IF-function which merges the content of A1 and B1 in C1
Selection.AutoFill Destination:=Range("C1:C2")
'You need to tell it how many rows to run it on ("C1: ")
Columns("C:C").Select
Selection.Copy
Columns("B:B").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Copies and pastes values from column C to column B
Columns("C:C").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft 'Deletes column C

End Sub

You can add nested IFs to handle more options.

Another option follows below:

Sub LoopChanges()

Range("B1").Select

Do Until ActiveCell.Offset(0, -1) = ""

If ActiveCell.Offset(0, -1) = "sietco" Then
ActiveCell = "eptbsiet"

End If

ActiveCell.Offset(1, 0).Select

Loop

End Sub

Again, you can add ELSEIFs to handle more options. I have attached the Excel file (Replace Text) which contains these macros.

I hope this helps.

Kind regards
Marius Barnard
Excel Trainer

Attached files...

Replace Text.xlsm

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Fill formulae across a sheet

To copy a formula down a spreadsheet where there is data underneath, to the left or to the right of the formula, double-click on the fill handle. The fill handle is the little black cross that appears in the bottom right-hand corner of the formula cell. Unfortunately, no similar facility exists to copy formulae across the sheet.

One reasonably quick way to copy an existing formula across a sheet is to select the formula and the cells on the right to which you want to copy it. Then press Ctrl+R to copy the formula across the selected range, or, if you are menu-minded, use the Edit|Fill|Right command.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.11 secs.