Convert Formula to VBA

mintz

Board Regular
Joined
Aug 5, 2015
Messages
129
Code:
=IFERROR(IFERROR(VLOOKUP(RIGHT(A1,LEN(A1)-FIND(",",A1)-1),2,FALSE),RIGHT(A1,LEN(A1)-FIND(",",A1)-1)),A1))

The formula takes the State from a City&State field (i.e. New York, NY) or in case there are multiple commas then it will take everything before the last comma i.e. Luganville, Santos, PR >> PR) or in case there are no commas it will keep the whole value in tact

How do I convert this formula to VBA?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi Mintz!

The easeest way to write this into the desired cell, hit enter. Start macro recorder -> click back to the cell (edit mode) -> hit enter again
The recorder will record smg like this:
Code:
sub mintzmacro()
    ActiveCell.FormulaR1C1 = _
        "=IFERROR(IFERROR(VLOOKUP(RIGHT(RC[-2],LEN(RC[-2])-FIND("","",RC[-2])-1),2,FALSE),RIGHT(RC[-2],LEN(RC[-2])-FIND("","",RC[-2])-1)),RC[-2])"
end sub
 
Upvote 0
Try this

Code:
Trim(Right(Replace([A1], ",", String(1000, " ")), 1000))


or

Code:
Dim r as Range
Set r = ActiveSheet.Range("A1")

Trim(Right(Replace(R, ",", String(1000, " ")), 1000))
 
Upvote 0
Try this

Code:
Trim(Right(Replace([A1], ",", String(1000, " ")), 1000))


or

Code:
Dim r as Range
Set r = ActiveSheet.Range("A1")

Trim(Right(Replace(R, ",", String(1000, " ")), 1000))
That worked great! How do I get the left part? (everything before the last comma)
I tried Left instead of Right, it works when there's one comma like New York, NY
but if there's something like Queens, New York, NY it just takes Queens
 
Upvote 0
Greetings mintz,

As a UDF maybe:

Rich (BB code):
Option Explicit
  
Public Function SplitLastDelimiter(ByVal Cell As Range, ByVal Delimiter As String) As Variant()
Dim LeftSide As Variant
Dim RightSide As Variant


  If InStr(1, Cell.Value, Delimiter) Then
    LeftSide = Trim(Left(Cell.Value, InStrRev(Cell.Value, Delimiter) - 1))
    RightSide = Trim(Mid(Cell.Value, InStrRev(Cell.Value, Delimiter) + 1))
  Else
    If Cell.Value = vbNullString Then
      LeftSide = vbNullString
    Else
      LeftSide = Cell.Value
    End If
    RightSide = vbNullString
  End If
  
  SplitLastDelimiter = Array(LeftSide, RightSide)
  
End Function

Where the formula would be array-entered into adjoining cells:
Excel Workbook
ABC
2My House, Phoenix, AZMy House, PhoenixAZ
3Phoenix, AZPhoenixAZ
4My House in Phoenix AZMy House in Phoenix AZ
Sheet3
Excel 2010
#VALUE!
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.

Does that help?

Mark
 
Upvote 0
Greetings mintz,

Does that help?

Mark

Thanks a lot mark, worked great for me!

Here's a different solution I managed to come up with
sState = Right([A1], Len([A1]) - InStrRev([A1], ","))sCity = Left([A1], InStrRev([A1], ",") - 1)
 
Upvote 0
Code:
=IFERROR(IFERROR(VLOOKUP(RIGHT(A1,LEN(A1)-FIND(",",A1)-1),2,FALSE),RIGHT(A1,LEN(A1)-FIND(",",A1)-1)),A1))

The formula takes the State from a City&State field (i.e. New York, NY) or in case there are multiple commas then it will take everything before the last comma i.e. Luganville, Santos, PR >> PR) or in case there are no commas it will keep the whole value in tact

How do I convert this formula to VBA?
Is your state always two letters long as shown? If so...

strState = Right(Range("A1").Value, 2)
 
Upvote 0
Thanks a lot mark, worked great for me!

Here's a different solution I managed to come up with

Code:
sState = Right([A1], Len([A1]) - InStrRev([A1], ","))
sCity = Left([A1], InStrRev([A1], ",") - 1)

Hi mintz,

You are very welcome. In regards to your alternate solution, while I would tend to avoid unnecessary use of Evaluate and particularly the shorthand form, I would suggest that regardless of methodology, you will want to include some type of test to ensure that the delimiter exists.

Mark
 
Upvote 0
The formula takes the State from a City&State field (i.e. New York, NY)
Another solution without VBA.
Based on example source data by @GTO

Try in B2 and copy down
Code:
=IF(ISNUMBER(FIND(",",RIGHT(MID(A2,1,LEN(A2)-3),1),1)),MID(A2,1,LEN(A2)-3-1),MID(A2,1,LEN(A2)-3))

Try in C2 and copy down
Code:
=IFERROR(MID(A2,FIND(CHAR(1),SUBSTITUTE(A2,",",CHAR(1),LEN(A2)-LEN(SUBSTITUTE(LOWER(A2),",",""))))+2,10),RIGHT(A2,2))
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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