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>
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

skorpionkz

Well-known Member
Joined
Oct 1, 2013
Messages
1,168
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
 

adema2508

New Member
Joined
Jan 22, 2014
Messages
4
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.
 

skorpionkz

Well-known Member
Joined
Oct 1, 2013
Messages
1,168
can you upload the sample of your file? There are many services which allowed you to do this (DropBox is one of them)
 

Watch MrExcel Video

Forum statistics

Threads
1,123,268
Messages
5,600,624
Members
414,394
Latest member
mahendar

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
Top