+ Reply to Thread
Results 1 to 8 of 8

Thread: VBA Function to replace an excel formula

  1. #1
    Registered User
    Join Date
    04-05-2009
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    44

    VBA Function to replace an excel formula

    Hi Everyone

    I am struggling to convert the following excel formula to a VBA function -

    =IF(ISERROR(SEARCH("-",A1)),IF(ISERROR(SEARCH(" ",A1)),A1,LEFT(A1,SEARCH(" ",A1)-1)),LEFT(A1,SEARCH("-",A1)-1))
    The formula refers to the same cell 7 times, so I wanted this to simplify it and replace with a VBA function. Basically the formula works on a string to find whether it has a space or hyphen. If the string has neither a space or hyphen then the function should return the string unchanged. If the string has either a space or hyphen, the characters left-side to the first space or hyphen should be returned. That's it!

    The excel formula above does this correctly. I just want this to convert to a VBA Custom Function. The attached excel file has a couple of failed attempts to get the function to work.

    Appreciate your help.

    Rakesh
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    12-14-2009
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    986

    Re: VBA Function to replace an excel formula

    Try this

    Function NumOnly(X) As Double
    Dim i As Integer
        For i = 1 To Len(X)
            If Mid(X, i, 1) = " " Or Mid(X, i, 1) = "-" Then
               NumOnly = Left(X, i - 1)
            End If
        Next i
    
        If NumOnly = 0 Then NumOnly = X
        
    End Function
    To thank someone who has helped you, click on the star icon below their name.

    I hate reading

    Portfolio

    I need a job.
    I am young and incompetent

  3. #3
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    703

    Re: VBA Function to replace an excel formula

    Hi,

    You can use this function

    Function testString(inputStr As String)
        firstSpace = InStr(inputStr, " ")
        firstHyphen = InStr(inputStr, "-")
        If firstSpace > firstHyphen And Not firstHyphen = 0 Then
            splitNum = firstHyphen
        Else
            splitNum = firstSpace
        End If
        testString = Right(inputStr, Len(inputStr) - splitNum)
    End Function
    Please take time to read the forum rules

  4. #4
    Registered User
    Join Date
    04-05-2009
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: VBA Function to replace an excel formula

    Thanks JieJenn, the function works for all the entries in the column, except for something like "123 X-Y", the function returns "#VALUE". Could you look into it, please?

  5. #5
    Registered User
    Join Date
    04-05-2009
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: VBA Function to replace an excel formula

    Quote Originally Posted by Steffen Thomsen View Post
    Hi,

    You can use this function

    Function testString(inputStr As String)
        firstSpace = InStr(inputStr, " ")
        firstHyphen = InStr(inputStr, "-")
        If firstSpace > firstHyphen And Not firstHyphen = 0 Then
            splitNum = firstHyphen
        Else
            splitNum = firstSpace
        End If
        testString = Right(inputStr, Len(inputStr) - splitNum)
    End Function
    Thanks Steffen, but your function does not work for "123-YY", "123 Y-Y" and "123 YY". the expected outcomes are "123" in all these cases. Could you relook?

    Thanks.

  6. #6
    Valued Forum Contributor
    Join Date
    12-14-2009
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    986

    Re: VBA Function to replace an excel formula

    Try the array formula. I think it is a better solution. Enter with Ctrl + Shift + Enter

    =LEFT(A2,MAX(IF(ISNUMBER(--MID(A2,ROW(INDIRECT("1:1024")),1)),ROW((INDIRECT("1:1024"))))))
    To thank someone who has helped you, click on the star icon below their name.

    I hate reading

    Portfolio

    I need a job.
    I am young and incompetent

  7. #7
    Registered User
    Join Date
    04-05-2009
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    44

    Re: VBA Function to replace an excel formula

    Quote Originally Posted by JieJenn View Post
    Try the array formula. I think it is a better solution. Enter with Ctrl + Shift + Enter

    =LEFT(A2,MAX(IF(ISNUMBER(--MID(A2,ROW(INDIRECT("1:1024")),1)),ROW((INDIRECT("1:1024"))))))
    Dear JieJenn, I am looking for a VBA Function, I got an excel formula working already. could you adjust your code a bit for me?

  8. #8
    Valued Forum Contributor
    Join Date
    12-14-2009
    Location
    San Francisco, CA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    986

    Re: VBA Function to replace an excel formula

    Your formula does not work with 1234 X-Y as you described in your post above. And I am kinda lazy to rewrite the array into a UDF........ I am sure others will be glad to jump in.
    To thank someone who has helped you, click on the star icon below their name.

    I hate reading

    Portfolio

    I need a job.
    I am young and incompetent

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0