How to add letter "P" at the end of cell text in column A IF it is value 0 in column B

scorpio3st

New Member
Joined
Sep 7, 2021
Messages
22
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi,

Please can you help me with creating a macro:
I have a numbers in cells in column A and need to add letter "P" at the end, but only for numbers witch have 0 in column B.
Example:
column A / column B column C
30454 / 0 30454P
360708 / 55 360708
Thanks.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I dont think you need macro for this try this

Book1
ABC
930454030454P
1036070855360708
Sheet1
Cell Formulas
RangeFormula
C9:C10C9=IF(B9=0,A9&"P",A9)
 
Upvote 0
Thanks. It is working perfect. Is there any way to add letter in column A without creating column C?
Thanks again.
 
Upvote 0
Thanks. It is working perfect. Is there any way to add letter in column A without creating column C?
Thanks again.

Try below Macro

VBA Code:
Sub Macro1()

' To Input "P" if the Column B as Zero Value

    Range("C1:C" & Range("A" & Rows.Count).End(xlUp).Row).Select
    Selection.FormulaR1C1 = "=IF(RC[-1]=0,RC[-2]&""P"",RC[-2])"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    Columns("C:C").Copy
    Columns("A:A").Select
    Selection.PasteSpecial Paste:=xlPasteValues
    Columns("C:C").Select
    Selection.Delete Shift:=xlToLeft
    Range("A1").Select
    
End Sub
 
Upvote 0
Here is another macro that you can try...
VBA Code:
Sub AddPifZero()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate(.Address & "&IF(" & .Offset(, 1).Address & "=0,""P"","""")")
  End With
End Sub
 
Upvote 0
Solution
Try below Macro

VBA Code:
Sub Macro1()

' To Input "P" if the Column B as Zero Value

    Range("C1:C" & Range("A" & Rows.Count).End(xlUp).Row).Select
    Selection.FormulaR1C1 = "=IF(RC[-1]=0,RC[-2]&""P"",RC[-2])"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    Columns("C:C").Copy
    Columns("A:A").Select
    Selection.PasteSpecial Paste:=xlPasteValues
    Columns("C:C").Select
    Selection.Delete Shift:=xlToLeft
    Range("A1").Select
   
End Sub
Excellent. working perfect.
 
Upvote 0
Here is another macro that you can try...
VBA Code:
Sub AddPifZero()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp))
    .Value = Evaluate(.Address & "&IF(" & .Offset(, 1).Address & "=0,""P"","""")")
  End With
End Sub
This works perfect too. Thanks a lot to all.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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