IF Statement with multiple or conditions

aquad6

New Member
Joined
Jun 23, 2023
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
Hi There!
I am trying to write a formula that allows me to place a dash before the last character in a cell text if it is an A,B,C etc. For example, if I have a sku that reads JH004A and I would like a formula that will populate another cell with JH004-A. IF it doesn't contain A,B,C etc. I want the other cell to read False. So far this formula =IF(AND(RIGHT(A2,1)="B"),LEFT(A2,LEN(A2)-1)&"-"&RIGHT(A2,1)) works but only if the end character is a "B", I can't figure out had to make it work if the end character is an A, or C. Any help would be greatly appreciated!! Thank you!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Book1
AB
2AAABAAA-B
3AAAAAAA-A
4AAACAAA-C
Sheet1
Cell Formulas
RangeFormula
B2:B4B2=IF(AND(OR(RIGHT(A2,1)={"A","B","C"})),LEFT(A2,LEN(A2)-1)&"-"&RIGHT(A2,1))
 
Upvote 0
One more question, If I have a sku that is MO456CD, is there a formula to input a dash in between the last numeric and first alphabet, so it reads MO456-CD. Some of my skus have two Alphabetical characters at the end while others have only one. Basically I'm looking for a formula that can put a dash in between the last numeric and first alpha character no matter how many Alpha characters are at the end of the sku.
 
Upvote 0
Amazing!!! Thank you sooo much!!!
One more question, If I have a sku that is MO456CD, is there a formula to input a dash in between the last numeric and first alphabet, so it reads MO456-CD. Some of my skus have two Alphabetical characters at the end while others have only one. Basically I'm looking for a formula that can put a dash in between the last numeric and first alpha character no matter how many Alpha characters are at the end of the sku.
 
Upvote 0
Book1
AB
2AAABAAA-B
3AAAAAAA-A
4AAACAAA-C
Sheet1
Cell Formulas
RangeFormula
B2:B4B2=IF(AND(OR(RIGHT(A2,1)={"A","B","C"})),LEFT(A2,LEN(A2)-1)&"-"&RIGHT(A2,1))
One more question, If I have a sku that is MO456CD, is there a formula to input a dash in between the last numeric and first alphabet, so it reads MO456-CD. Some of my skus have two Alphabetical characters at the end while others have only one. Basically I'm looking for a formula that can put a dash in between the last numeric and first alpha character no matter how many Alpha characters are at the end of the sku.
 
Upvote 0
Hi There!
If I have a sku that is MO456CD, is there a formula to input a dash in between the last numeric and first alphabet, so it reads MO456-CD. Some of my skus have two Alphabetical characters at the end while others have only one. Basically I'm looking for a formula that can put a dash in between the last numeric and first alpha character no matter how many Alpha characters are at the end of the sku.
 
Upvote 0
How about
Excel Formula:
=REPLACE(A2,MAX(IFERROR(FIND(SEQUENCE(,10,0),A2),0))+1,0,"-")
 
Upvote 0
Some of the guys here will have a more elegant and smaller solution.

MrExcelPlayground18.xlsx
AB
1MO456CDMO456-CD
2MX123AMX123-A
3123AM123-AM
412AB23C12AB23-C
Sheet8
Cell Formulas
RangeFormula
B1:B4B1=LET(z,A1,s,SEQUENCE(1,LEN(z)),a,MAX(ISNUMBER(VALUE(MID(z,s,1)))*s),LEFT(z,a)&"-"&RIGHT(z,LEN(z)-a))
 
Upvote 0
Your formulas would start to get pretty long & difficult to read if you used purely native Excel functions for this. There may be some of the newer 365 functions that may do it simpler, but formulas are not my forte. Personally, I would use a UDF to do this. Place the following code in a standard module & see what you think.
VBA Code:
Function InsertDash(r As Range) As String
    Dim s As String, i As Long, j As Long
    s = Trim(r.Value)
    For i = Len(s) To 1 Step -1
        If IsNumeric(Mid(s, i, 1)) Then
            j = i
            Exit For
        End If
    Next i
    If j = 0 Or j = Len(s) Then
        InsertDash = r.Value
        Exit Function
    Else
        s = Left(s, j) & "-" & Right(s, Len(s) - j)
    End If
    InsertDash = s
End Function

In use:
Book1
AB
1MO456CDMO456-CD
2AAA1BAAA1-B
3A3AAAA3-AAA
4AAAC123AAAC123
512abc12-abc
61A2B3CCC1A2B3-CCC
Sheet1
Cell Formulas
RangeFormula
B1:B6B1=InsertDash(A1)
 
Upvote 0

Forum statistics

Threads
1,223,768
Messages
6,174,411
Members
452,562
Latest member
Himeshwari

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