Extract characters from a Cell using a VBA Formula

Kurt

Well-known Member
Joined
Jul 23, 2002
Messages
1,664
I have the following VBA code:

Code:
AddFormula TopLeft.Offset(1, 1).Resize(RowCount, 1), "=IFERROR(LEFT(AA" & Row & ",FIND(""`"",AA" & Row & ")-1),"""")"

This is the cell it is looking at:
Code:
MECH~CDA-CUP-PF~1 - CUP0915.2XL - Copper Reducer (P)

I want the formula to extract the first characters before the ~ regardless if it is 3 or 4 or however many characters.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
A formula like
=LEFT(A1, FIND("~",A1&" ~")-1)

will do what you want. Note that the first literal is one tilde, the second is [space][tilde]
 
Upvote 0
A formula like
=LEFT(A1, FIND("~",A1&" ~")-1)

will do what you want. Note that the first literal is one tilde, the second is [space][tilde]

I check the code and I had a wrong character there which I suspected!

Thank you for the confirmation Mike!
 
Upvote 0
another Option
Code:
Sub addFormulaArray()


Cells.Clear
    [A1] = "MECH~CDA-CUP-PF~1 - CUP0915.2XL - Copper Reducer (P)"
    
     Range("B1:K1").FormulaArray = "=IFERROR(MID($A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B3)+1))),FIND(""~"",$A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B3)+1))))-FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B3)+1)))),"""")"
    
    ' or see 2nd line
    Range("B2:D2").FormulaArray = "=LEFT(A1, FIND(""-"",A1&""-"")-1)"


    Range("B2").Replace What:="A1", Replacement:="IFERROR(MID($A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B1)+1))),FIND(""~"",$A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B1)+1))))-FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B1)+1)))),"""")", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Find(What:="A1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
        
End Sub
 
Last edited:
Upvote 0
another Option
Code:
Sub addFormulaArray()


Cells.Clear
    [A1] = "MECH~CDA-CUP-PF~1 - CUP0915.2XL - Copper Reducer (P)"
    
     Range("B1:K1").FormulaArray = "=IFERROR(MID($A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B3)+1))),FIND(""~"",$A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B3)+1))))-FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B3)+1)))),"""")"
    
    ' or see 2nd line
    Range("B2:D2").FormulaArray = "=LEFT(A1, FIND(""-"",A1&""-"")-1)"


    Range("B2").Replace What:="A1", Replacement:="IFERROR(MID($A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B1)+1))),FIND(""~"",$A1,FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B1)+1))))-FIND(""|"",SUBSTITUTE(""~""&$A1,""~"",""|"",(COLUMN()-COLUMN($B1)+1)))),"""")", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Cells.Find(What:="A1", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Activate
        
End Sub

Thank you for your efforts but I need to stay with the format I am using.

That will not work for my present needs. Too many lines of code also, but thank you for your efforts.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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