+ Reply to Thread
Results 1 to 6 of 6

Create VBA function using worksheet functions?

Hybrid View

  1. #1
    Registered User
    Join Date
    12-23-2010
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    66

    Create VBA function using worksheet functions?

    Hello,

    How do I create a VBA function using worksheet functions? For example, let's say I use the Macro Recorder and type in a worksheet function. In VBA, it creates a Procedure as opposed to a Function. For example, it may create this:

    Sub dayttest()
        ActiveCell.FormulaR1C1 = _
            "=IF(DAY(RC[-11])<10,CONCATENATE(0,DAY(RC[-11])))"
    End Sub
    How do I convert that into a VBA function? I have a worksheet function that's very long and it would be nice if I just simplify it in VBA.

    Thanks!
    Last edited by Leith Ross; 12-27-2012 at 07:53 PM. Reason: Added Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Create VBA function using worksheet functions?

    Hello anon,

    You need to know both formulas and VBA to translate from one to the other. The problem here is that you are referencing the ActiveCell, which can be anywhere on the sheet. From that cell you are moving 11 columns to left. If your ActiveCell is not in column "L" or further to the right, an error will occur because you are trying to reference a non existent column.

    To really help you with this problem will require seeing the workbook. If you post a copy then it will be possible to provide you with code that actually works.

    To Post Your Workbook
    1. At the bottom right of the Reply window, Click the button Go Advanced
    2. At the top of the Your Message Window, in the first row of icons, Click the Paperclip icon.
    3. Click the Add Files button at the top right in the dialog box. This displays the File Manager dialog.
    4. Click the Select Files button at the bottom middle of the dialog.
    5. In the new window Find the file you want to upload, select it, and Click Open.
    6. You will now be back in the File Manager dialog. Click the bottom Middle button Upload File.
    7. Wait until the file has completely uploaded before you exit the File Manager dialog.

    File Manger Pic
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-23-2010
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    66

    Re: Create VBA function using worksheet functions?

    Hello,

    Thanks for the tips. While VBA is definitely not necessary for what I am trying to do, I would like to do it in VBA so that I can learn VBA. I have attached the file and the right most column you can see my longish formula. Basically I want to condense that into one VBA formula, not procedure.

    Thanks!

    Anon Workbook.xlsx

  4. #4
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    610

    Re: Create VBA function using worksheet functions?

    Create a UDF with the following code (place into a module)
    Function MYFUNCTION(stockdate As Date, optionsymbol As String)
    Dim tmpStr As String
    If Month(stockdate) < 10 Then
        tmpStr = "0" & CStr(Month(stockdate))
        Else
        tmpStr = CStr(Month(stockdate))
    End If
    If Day(stockdate) < 10 Then
        tmpStr = tmpStr & "0" & CStr(Day(stockdate))
    Else
        tmpStr = tmpStr & Day(stockdate)
    End If
        
    MYFUNCTION = optionsymbol & Right(Year(stockdate), 2) & tmpStr
    End Function
    So the formula would be =MYFUNCTION(C2,E2)
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated?

  5. #5
    Registered User
    Join Date
    12-23-2010
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    66

    Re: Create VBA function using worksheet functions?

    Thanks for the help guys. Both work great. I'll take a look at the VBA code and dissect it to gain a better understanding. The shortern concatenate formula also works great. Didn't know about the text function.

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Create VBA function using worksheet functions?

    maybe so (for N3)
    Formula: copy to clipboard
    =CONCATENATE(E3,TEXT(C3,"YYMMDD"))

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1