Vba code for find and replace

adema2508

New Member
Joined
Jan 22, 2014
Messages
4
Good day all,

Please can someone in the house help me with a code to find and replace any "D" that comes after every "P" with an "N". see sample of data that i am working on below.

JAN FEBMARAPRMAYJUNJULAUGSEPOCT
NDDDDDDDDD
NDP DDP DDPN
NP P P DDPP DD
NP P DDP NP DP

<colgroup><col><col><col><col><col><col><col><col><col><col></colgroup><tbody>
</tbody>
regards.

<colgroup><col span="10"></colgroup><tbody>
</tbody>
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Adema2508,

Assuming that JAN is in the cell A1, please check below code.

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub test()
Dim icol As Long, irow As Long

irow = 2
Do Until Cells(irow, 1) = Empty
    icol = 2
    Do Until Cells(2, icol) = Empty
        If Cells(irow, icol) = "D" And Cells(irow, icol - 1) = "P" Then Cells(irow, icol) = "N"
        icol = icol + 1
    Loop
    irow = irow + 1
Loop

End Sub
 
Upvote 0
Thank you Skorpoinkz,

it seems not to be working, or may be i did apply the code correctly, and also the data i am working with has about 200 rows (plenty data). may be this might throw more light on the question.

thanks.
 
Upvote 0
can you upload the sample of your file? There are many services which allowed you to do this (DropBox is one of them)
 
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

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