+ Reply to Thread
Results 1 to 16 of 16

VBA array Function help

  1. #1
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    VBA array Function help

    Hi All,

    I'm struggling with a particular issue that I dont seem to be able to resolve.

    in the attached workbook there is a simple time sheet arrangement whereby there is a name, which is arranged in a column, a date row across the top, and hours are inserted against each day worked.

    there is a rather tricky chargeout structure which I'm trying to accommodate in a function. I've managed to do a formula but it is very large, and also needs a second row at the minute because Ive not managed to convert it into an array.

    the target is to have one cell contain a total charge out value, in a column next to the column containing the name, without having to have subtotals etc

    I thought I'd just about cracked the code for a single calculation (which would need a subtotal) but the function just returns a value error. i'm new to VBA and really struggling to see where I've gone wrong, and where to start to turn this into an array using the date and hours as arrays, and the name as a fixed value.

    any help would be very gratefully received

    regards
    Attached Files Attached Files
    Last edited by robwebber; 03-10-2015 at 04:33 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,501

    Re: VBA array Function help

    OK, this gives an answer, not sure if it's the right answer.

    Please Login or Register  to view this content.

    Regards, TMS
    Attached Files Attached Files
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA array Function help

    Thanks TMS, its not giving the right answer, but you dont seem to have changed my code that much so maybe its something I've caused. - the answer that the code should be delivering is in row 11 in the file I uploded, Would you advise what the DBl1 is for in the code? it doesnt seem to change anything whether it is there or not.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,501

    Re: VBA array Function help

    No, no intentional changes to your code. The only "big" change was to the check for a holiday date. You probably need to do some more exhaustive testing

    Right, this version works and gives the right answers for the sample data.

    Please Login or Register  to view this content.

    Regards, TMS

  5. #5
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA array Function help

    Thank you for that.

    it didnt quite work when I tested the calc on days which were holidays, but changing the statement " IF vbhol <>"" Then" TO "IF vbhol = 0 Then" did the trick. I assume the "on error goto 0" statement means that if the result (of the check against the holiday days) is an error then return zero?

    I'm very grateful for the help you have provided. I've a bit of reading up to do on the code used to deal with the holidays.

    If you fancy a challenge, would you know how to alter the code to make it work with an array of dates and times worked? so that the total could (for example) go in B10, and then I could do away with the total price for the day line altogether.

    thanks again !
    Last edited by robwebber; 03-11-2015 at 06:23 AM.

  6. #6
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA array Function help

    OOps,

    Spoke too soon, holidays work but all other days now use OT2 rate too,
    Last edited by robwebber; 03-11-2015 at 06:33 AM.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,501

    Re: VBA array Function help

    I assume the "on error goto 0" statement means that if the result (of the check against the holiday days) is an error then return zero?
    No. It switches off the error handling. When you say "On Error Resume Next" you are using one of the resume options which is simply to carry on with the next statement as if the error didn't happen. You can then test the error condition, if you wish, and switch error trapping off, again, if you wish.

    Problem was with the holiday lookup. Fixed.

    Please Login or Register  to view this content.

    If you fancy a challenge, would you know how to alter the code to make it work with an array of dates and times worked? so that the total could (for example) go in B10, and then I could do away with the total price for the day line altogether.
    No, not me, I'm afraid. Could be just another function that loops through the array passing name, date and hours to the existing function. But, for simplicity, I think you'd need to repeat the name for each date/hours pair.


    Regards, TMS
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA array Function help

    Thanks very much..... Genius. all working now.

    (Thanks for the array advice too.)

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,501

    Re: VBA array Function help

    You're welcome. Thanks for the rep.

    And here is the function you asked for:

    Please Login or Register  to view this content.

    Not sure how it is an improvement on what you already have but, what do I know do?


    Regards, TMS
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA array Function help

    Thats almost there,

    I wanted to use it in the horizontal plain rather than vertical, and with a single reference to the name of the employee but that gives me a good starting point.

    without a price per day row, i can have one row for each employee, which will look pretty much like a time sheet but the total column reflects the total charge out price for all time spent rather than total hours. total hours giving me the problem of categorising the hourly rates for all of the differing potential combinations of day and working time etc.

    now that its working i've realised that if I add to the ranges (add new employee's or holidays) after the sheet has initially been set up, I have a refresh problem. hitting recalculate doesn't seem to incorporate the new data into the calculation only entering the worksheet cell containing the individual formula and hitting enter refreshes it fully. I don't know how to dealt with that!

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,501

    Re: VBA array Function help

    Please Login or Register  to view this content.

    Because the function refers to cell/range references, I would have expected it to adjust and recalculate automatically. However, you could try adding
    Please Login or Register  to view this content.
    as the first line of code in the function. That should cause it to recalculate when Excel feels a recalculation is in order.


    Regards, TMS

  12. #12
    Registered User
    Join Date
    03-11-2015
    Location
    India
    MS-Off Ver
    doo
    Posts
    1

    Re: VBA array Function help

    Thanks. I was searching the same information since many months. Thanks for your hard work.



    SEO Freelancer
    Camino Inca Peru
    Camino Inca Machupicchu

  13. #13
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA array Function help

    This minor alteration allows me to use a single cell for the name and an array for date and hours.

    not fully tested but it gives me an answer and it matches that which i would have expected.

    your replies have been a great help. I'm pretty sure I'd have been here next year trying to work this one out!!

    Option Explicit

    Function MyPayTotals(ByRef rNames As String, _
    ByRef rDates As Range, _
    ByRef rHours As Range)

    Dim dTotal As Double
    Dim i As Long


    For i = 1 To rDates.Cells.Count
    dTotal = dTotal + MyPay(rNames, _
    rDates(i), _
    rHours(i))
    Next 'i

    MyPayTotals = dTotal

    End Function

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,501

    Re: VBA array Function help

    @robwebber: and is that different to the code I posted?

  15. #15
    Registered User
    Join Date
    03-13-2014
    Location
    Leeds, England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA array Function help

    Trevor You are a true Guru.

    Target very much achieved in Full. - I'm very grateful!

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,501

    Re: VBA array Function help

    Thank you

    Please note that you should add Code Tags to post #13.


    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 13
    Last Post: 04-08-2014, 05:46 AM
  2. [SOLVED] IF Function referencing IsNumber, Match, Left function on separate sheets
    By Touch9713 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-12-2013, 10:09 PM
  3. [SOLVED] Using Offset function as the array in the PercentRank function is giving wrong result
    By Bobneil in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 08-06-2013, 09:29 PM
  4. Replies: 1
    Last Post: 03-21-2012, 11:22 AM
  5. Replies: 2
    Last Post: 03-20-2009, 01:29 PM

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