How to increment a number in one cell based on a letter in another

Seniornetman

New Member
Joined
Dec 14, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I want to increment a number in one cell based on the letter P, T or A in another cell. How can I go about this?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Do you mean like this?

Book1
AB
1Letter
2PP1
3TT1
4PP2
5AA1
6TT2
7PP3
Sheet1
Cell Formulas
RangeFormula
B2:B7B2=IF(A2="","",A2&COUNTIF($A$2:A2,A2))
 
Upvote 0
Upvote 0
That will take VBA. With this if you enter P, T or A in B2. A2 will increment by 1

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, s
Set c = Intersect(Target, Range("B2"))
If c Is Nothing Then Exit Sub
s = UCase(c)
If s = "P" Or s = "T" Or s = "A" Then
    Application.EnableEvents = False
        Range("A2") = Range("A2") + 1
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
No I mean the number 1 in A and it increments to 2 when P is entered in B

That will take VBA. With this if you enter P, T or A in B2. A2 will increment by 1

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, s
Set c = Intersect(Target, Range("B2"))
If c Is Nothing Then Exit Sub
s = UCase(c)
If s = "P" Or s = "T" Or s = "A" Then
    Application.EnableEvents = False
        Range("A2") = Range("A2") + 1
    Application.EnableEvents = True
End If
End Sub
OK I see what you have done here and I assume since my cells are G2 and L2 all I have to do change those B2 to L2 and A2 to g2 and it should work, correct? Also I assume since this will need to work in 38 cells I will re edit and put it in all 38?
 
Upvote 0
No, the code will just need to be modified to work with that entire range, give me a minute.
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range, s
Set c = Intersect(Target, Range("L2:L38"))
If c Is Nothing Then Exit Sub
For Each d In c
    s = UCase(d)
    If s = "P" Or s = "T" Or s = "A" Then
        Application.EnableEvents = False
            d.Offset(, -6) = d.Offset(, -6) + 1
        Application.EnableEvents = True
    End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
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