+ Reply to Thread
Results 1 to 11 of 11

Convert Formula to VB Module

  1. #1
    Registered User
    Join Date
    01-01-2013
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010 (work) & 2013 (home)
    Posts
    45

    Convert Formula to VB Module

    Hi Guys,

    I'm trying to convert a formula I've created, into a VB Function i.e.

    Function hoursWorked(Shift)

    My initial thought was to use 'Paste Formula, but then I can't define Shift anywhere.

    Here is the formula anyway, I hope somebody can help:

    =IF(SUM((VLOOKUP(MID(C22,7,2),hourtable,2,FALSE)+VLOOKUP(MID(C22,10,2),mintable,2,FALSE))-(VLOOKUP(MID(C22,1,2),hourtable,2,FALSE)+VLOOKUP(MID(C22,4,2),mintable,2,FALSE)))>=8,SUM((VLOOKUP(MID(C22,7,2),hourtable,2,FALSE)+VLOOKUP(MID(C22,10,2),mintable,2,FALSE))-(VLOOKUP(MID(C22,1,2),hourtable,2,FALSE)+VLOOKUP(MID(C22,4,2),mintable,2,FALSE)))-0.5,SUM((VLOOKUP(MID(C22,7,2),hourtable,2,FALSE)+VLOOKUP(MID(C22,10,2),mintable,2,FALSE))-(VLOOKUP(MID(C22,1,2),hourtable,2,FALSE)+VLOOKUP(MID(C22,4,2),mintable,2,FALSE))))
    Basically I need to replace all references to C22, with Shift, and save it as a Module.
    Elliot
    The geek shall inherit the earth

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Convert Formula to VB Module

    This line of code should do it

    Please Login or Register  to view this content.
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    01-01-2013
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010 (work) & 2013 (home)
    Posts
    45

    Re: Convert Formula to VB Module

    I've used that:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    And now I'm getting #VALUE

  4. #4
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Convert Formula to VB Module

    The syntax is wrong for a UDF .. Try:-

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    01-01-2013
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010 (work) & 2013 (home)
    Posts
    45

    Re: Convert Formula to VB Module

    When I run that the debugger says the function isn't defined and Highlights VLookup. Would I need to specify WorksheetFunction more than once?

  6. #6
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Convert Formula to VB Module

    Ok... That's because every reference to an inbuilt worksheet function needs to be preceeded with "Worksheetfunction.". This could get very messy

    It's probably more code efficient just to use VBA code rather than worksheet functions inside a UDF.. Can you upload a file ?

  7. #7
    Registered User
    Join Date
    01-01-2013
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010 (work) & 2013 (home)
    Posts
    45

    Re: Convert Formula to VB Module

    I've uploaded the sample sheet
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Convert Formula to VB Module

    Try this for your UDF

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-01-2013
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010 (work) & 2013 (home)
    Posts
    45

    Re: Convert Formula to VB Module

    That's great gives the value back - but the reason for the IF, was when it returns 8 or more hours, take 0.5 off to allow for lunch. Not sure how that could be added?
    I tried adding underneath
    If HoursWorked >= 8 Then HoursWorked - 0.5 Else HoursWorked
    but it didnt like that ^_^
    This is great though :D

  10. #10
    Registered User
    Join Date
    01-01-2013
    Location
    Manchester, England
    MS-Off Ver
    Excel 2010 (work) & 2013 (home)
    Posts
    45

    Re: Convert Formula to VB Module

    Ooh I managed it

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Is my final result. Thanks for your help :D

  11. #11
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Convert Formula to VB Module

    Nice one.... Antime

+ 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