+ Reply to Thread
Results 1 to 29 of 29

Networkday function in Macro not working

  1. #1
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Networkday function in Macro not working

    I am trying to perform a calculation in VBA and insert it in a cell. Everything seems to work fine until I try to incorporate using the NetworkDays function. What I am trying to accomplish is to divide Textbox4.Value by the difference of TextBox2.Value (i.e. which is a date by not sure if it is being read as a date) and today's date multiplied by 8.

    An example would be Textbox4.Value = 100 and Textbox2.Value = 07/30/2017.....Today is 06/27/2017....(i.e. using Networkdays formula in excel without any holidays this would be 24 days)

    100/(24*8)

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Networkday function in Macro not working

    Try replacing NetworkDays with Application.NetworkDays.
    If posting code please use code tags, see here.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Networkday function in Macro not working

    Hi,

    If you want to use a worksheet function in VBA then you need to qualify it with the WorksheetFunction syntax. So try

    ....WorksheetFunction.NetworkDays(...etc...

    Otherwise upload the workbook so that we can see in context.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Quote Originally Posted by Norie View Post
    Try replacing NetworkDays with Application.NetworkDays.
    This provided me a value but does not seem to be the right value....not sure if my formula is wrong or if it is not giving me the desired output thus making the calculation wrong. If interested I have attached the workbook. Code is found in UserForm1 CommandButton1.

  5. #5
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Richard Buttrey,

    Thanks..I actually tried the the application structure but am not getting the right output. See attached workbook if interested in helping further if not thanks thus far for the assitance.

    Jared
    Attached Files Attached Files
    Last edited by jeffreybrown; 06-27-2017 at 05:43 PM. Reason: Removed full quote

  6. #6
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Quote Originally Posted by Norie View Post
    Try replacing NetworkDays with Application.NetworkDays.
    I worked from the Application.Networkdays out and I think the problem is that the Application.Networkdays is providing a negative value I need it to be positive.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Networkday function in Macro not working

    Hi,

    What do you mean by 'the right output'?
    Since the system is generating numbers We need to know what answers you expect for some typical inputs. Can you give some examples?

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,530

    Re: Networkday function in Macro not working

    A negative value just means your StartDate is larger then your BeginDate.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  9. #9
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Networkday function in Macro not working

    You need to swap your variables in NetworkDays()

    Please Login or Register  to view this content.
    Splitting it up and checking at each stage revealed the error.

    Please Login or Register  to view this content.
    Last edited by jeffreybrown; 06-27-2017 at 05:43 PM. Reason: Applied code tags

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,530

    Re: Networkday function in Macro not working

    Hence my reply.

    Safer will also be

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Stormin,

    Thanks for the reply as I have corrected now the issue seems to be in applying the addition sequence. As I worked from the exponent out everything seems to workout correctly until I try to mutiply the sum of (A+B) to the equation. Is there something specific I need to do as this is not making any sense.

    This might be outside the scope of this thread but I figured I would ask. If not I appreciate all your guys help.

    Jared
    Last edited by jeffreybrown; 06-27-2017 at 05:42 PM. Reason: Removed full quote

  12. #12
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    I actually modified the addition using the suggestion above in which I used a worksheetfunction.Sum code to perform the addition which gave me the value I wanted. Not sure if this is the best way to do it or if anyone would have thoughts on how that would be the "wrong" way to do it.

    Please Login or Register  to view this content.
    Jared

  13. #13
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Networkday function in Macro not working

    You shouldn't get different results between A + B and SUM(A, B). Check the values of each variable to see if they are as expected, then check the datatypes of each

    Please Login or Register  to view this content.
    Last edited by jeffreybrown; 06-27-2017 at 05:42 PM. Reason: Code tags please

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,530

    Re: Networkday function in Macro not working

    In VBA try to avoid the use of Worksheetfunction if there's a Application_variant available.
    The use of Worksheetfunction always results in code going into debug in case of error. Application just holds the error.

  15. #15
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Stormin,

    I reviewed it multiple times with and without the (A+B) multiplication and it would never work. Finally I applied the Worksheet function for SUM and it did...I was baffled but I am sure there is a reason as I am not the greatest at VBA.
    Last edited by jeffreybrown; 06-27-2017 at 05:41 PM. Reason: Removed full quote

  16. #16
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    bakerman2,

    Is there an application variant for performing addition?

    This is what I have currently that works but only with the worksheet function:

    Please Login or Register  to view this content.
    Last edited by jeffreybrown; 06-27-2017 at 05:41 PM. Reason: Removed full quote

  17. #17
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,530

    Re: Networkday function in Macro not working

    In your code only F is declared as integer, all the rest is declared as Variant.
    The correct way is
    Please Login or Register  to view this content.
    It's confusing when later on you set each variable as a string.
    Please Login or Register  to view this content.
    will do.
    There is a
    Please Login or Register  to view this content.
    variant.

  18. #18
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    bakerman2,

    The reasoning for that is because I am still fairly new to all of this and really only have been learning by example so I appreciate the clarification. I guess to not string this out if I am understanding correctly my issue was probably because A and B were variants not integers. Based on what your suggesting is the default that they are "Dim" as Variant?

    Also you suggest I set them as a string is that because I put them in quotations (which inevitably is probably becuase it didnt work because I didn't set them appropriatley as integers)

    Jared
    Last edited by jeffreybrown; 06-27-2017 at 05:40 PM. Reason: Removed full quote

  19. #19
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,530

    Re: Networkday function in Macro not working

    Yes, because when you don't specify a type they are set as Variant.

  20. #20
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Networkday function in Macro not working

    Hi Jared,

    Please refrain from quoting full quotes as it just causes clutter in the thread. Only quote that which is necessary and relevant to continue the thread.
    HTH
    Regards, Jeff

  21. #21
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Networkday function in Macro not working

    Hi Jared,

    (Edit: note that I hadn't read any of the replies today before I posted this, so some points have already been mentioned)

    I had a look at your file, more specifically the code behind your userform in Private Sub CommandButton1_Click()
    There are a couple things that I can see that will need correcting for the code to work properly.

    1.
    Your declaration at the top is a common mistake that is made when first learning VB. I've done it, most people have done it, and you don't realise until something goes wrong. You have:
    Please Login or Register  to view this content.
    which you would intuitively expect to declare all the variables as integers. However the comma separates each statement, so everything between the commas is in effect a separate statement. So only "F as Integer" classes as integer. When you do not specify a variable type it will default to Variant which lets VB choose the variable type based on the input. In general, unless you specifically need a type Variant then you should avoid this and explicity state each variables type. So A, B, C, D, E are all type Variant by default. You will need to change this section to:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Personally I find the first style is easier to upkeep and read. Programmatically they function the same.

    2.
    In your SELECT CASE statements you are setting your variables to numbers surrounded by quotation marks. Quotation marks tell VB that whatever is inside should be treated as a text string. For example,
    Please Login or Register  to view this content.
    should instead be:
    Please Login or Register  to view this content.
    This sets the variable A with a true number (not a number stored as text). The problem you were having with your (A + B) statement was the result of the combination of points 1 and 2. Because A and B were both set to type Variant, when you then set e.g. A = "10", VB saw that you told it that "10" is a text string, and assigned the type String to variable A. The same thing happened with B. Then you tried to 'add' two strings together which results in concatenation, one string after another, i.e. "10" + "5" = "105".

    3.
    In your long calculation formula you are taking a values from a TextBox, which will return strings. You must make sure you are inputting the correct data type into your formula, or errors such as above will occur. So for example instead of
    Please Login or Register  to view this content.
    which you want to be a number, you should use
    Please Login or Register  to view this content.
    which ensures that you are using a number and not a text string.

    Likewise, as bakerman2 said in his post, it is better to change
    Please Login or Register  to view this content.
    which you want to be a date (but the user can input anything at all), you should use
    Please Login or Register  to view this content.
    which ensures that what the user entered can be converted to a date serial.
    If it can't be converted then it will throw an error. As an upgrade you can insert some code at the beginning that checks this, and then lets the user retry if they have inserted an invalid date.

    After these tweaks your full equation should look like:
    Please Login or Register  to view this content.
    although in this form it will be a nightmare to maintain, as you may have found hehe

    (Optional) Personally I would upgrade this to make it more human readable. I would set variables to each part with a readable name, build the parts, and then execute the equation. So I'd do something similar to this:
    Please Login or Register  to view this content.
    It all depends on how you interpret your own equation.
    Note that Double is a number that can have decimal places, unlike Integer which cannot.

    If you correct the non-optional points 1, 2, and 3 then your code should work


    (Optional) As a tip, when writing SELECT CASE statements you can group cases together when you want multiple cases to have the same function. For example, you wrote:
    Please Login or Register  to view this content.
    which can be shortened (and corrected) to:
    Please Login or Register  to view this content.
    It is a lot easier to see the criteria for when a certain case is applied this way. Also notice that I indented each level of the statement (by selecting all the lines and pressing TAB) to make it easier to read, which is good practice and very helpful when your statements start getting larger and more complicated.

    I would also rename all the input boxes in the UserForm to have meaningful names, as ComboBox3 does not tell you which box it relates to unless you go back to the UserForm and select it.



    I hope this helps to increase your VB knowledge
    Last edited by Stormin'; 06-28-2017 at 05:23 AM.

  22. #22
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,530

    Re: Networkday function in Macro not working

    @Stormin'

    I would also rename all the input boxes in the UserForm to have meaningful names, as ComboBox3 does not tell you which box it relates to unless you go back to the UserForm and select it.
    This would be open to discussion because when performing searches and then filling Textboxes it's much more easy to loop through then when each TextBox has a meaningfull name, you have to specify them one by one.
    Same goes when writing to worksheet, much easier in a loop then specifyng them one by one.

  23. #23
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Quote Originally Posted by bakerman2 View Post
    Yes, because when you don't specify a type they are set as Variant.
    Bakerman2,

    Thank you so much for the clarification and the assistance with this project. Your help was very much appreciated.

    Jared

  24. #24
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Quote Originally Posted by jeffreybrown View Post

    Please refrain from quoting full quotes as it just causes clutter in the thread.
    My apologies for this as I can definetly respect where your coming from as it helps when others search and read an old thread via google. Thanks for the advice.

    Jared

  25. #25
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Quote Originally Posted by Stormin' View Post
    Hi Jared,

    I hope this helps to increase your VB knowledge
    Stormin,

    Words can't express my graditude for you taking the time to write all of that information out as although I was starting to catch on to the problem this post was very insightful on helping me understand the "why" aspect of much of the problems I was having as well as that maybe I need to be a little more attentive to "cleanliness" in my vba code as I tend to just patch things together from previous work I have done. I have adapted to all of your suggestions and again just wanted to explain my utmost appreciation for the assistance and explanation.

    Jared

  26. #26
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,530

    Re: Networkday function in Macro not working

    You're more then welcome and thanks for the rep+.

  27. #27
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Quote Originally Posted by Stormin' View Post
    Hi Jared,
    Stormin,

    I was unsure if I could reopen the thread for this may be worthy of a different thread as it is essentially a modification of the code we worked on. The current code gives does a little opposite of what I intended when NetworkDays is 0 or negative (this meaning due today or past today's date). I am not sure what it is doing as I have not got that far in the math but I am assuming it is either taking for example -1 days * 8 hours = -8 hours then dividing that by the EstWrkHours which would give you a negative dblPowerToE. My intent would be to actually increase the exponent if it is 0 or negative. This would be done by if the value is 0 or past due subtracting from the EstWorkHrs then dividing the EstWorkHrs by this new number. It may be easier through example.

    Lets use 100 EstWrkHrs:

    Current Code works for positive values so lets start with 40 workdays out

    dblPowerToE = 1+(100/(40*8))=1+(100/320)=1+.3125=1.3125 This works how intended

    Current Code for 0 workdays out

    dblPowerToE = 1+(100/(0*8))=1+(100/0)= The final product in column C is 0 but I am assuming thats becasue its an error becasue 100/0 is an error

    Current Code for -1 workdays

    dblPowerToE = 1+(100/(-1*8))=1+(100/-8)=1+-12.5=-11.5 This does not work.

    Desired code for 0 or Negative Workdays out

    dblPowerToE = 1 + (EstWrkHrs/(EstWrkHrs+(Networdays*8)))

    Example -2 workdays out with 100 estwrkhrs

    dblPowerToE = 1 + (100/(100+(-2*8))) = 1 + (100/(100+-16) = 1+(100/84) = 2.19

    As the negative value that you add in the divisor (i.e. the bottom number) gets higher you will reach a point at which where you meet 0 or go negative at this point I would want the code to populate cell C with text such as Error. The thought process here is it is going to exponentially increase the score until you have reached a point where the amount of hours overdue is the same as the amount of hours that was originally to be required. Example (100/(100+-100)) = (100/0) or even further (100/(100+-105)) = (100/-5) <----although this will give a value I want to provide error after we have reached the point of going past a 0 divisor.

    If this is to much I will make a new thread just let me know I just figured as you had some intimate knowledge of waht I was trying to accomplish it may not be that hard.

    Jared
    Last edited by jaredmccullough; 06-28-2017 at 05:58 PM.

  28. #28
    Valued Forum Contributor
    Join Date
    03-16-2017
    Location
    UK
    MS-Off Ver
    2016
    Posts
    371

    Re: Networkday function in Macro not working

    Hi Jared,

    Happy to help. This is how we all get better... the saying goes coding to learn is better than learning to code.


    You are correct in your debugging of zero and negative dblNetWorkDays. dblNetWorkDays = 0 will make dblAvailHours = 0 which causes a divide by zero error. Negative dblNetWorkDays will make (dblEstWorkHrs / dblAvailHours) negative too, and raising to a negative power takes the inverse of the positive result. I'm sure you're aware that x-3 = 1/(x3).

    To use the negative / zero result as a measure of 'overdue-ness', and hence exponentially increase the priority, you can use an IF...THEN statement to change your dblAvailHours according to your desired logic.

    i.e. change your current code snippet from
    Please Login or Register  to view this content.
    to check dblNetWorkDays before it calculates dblAvailHours with:
    Please Login or Register  to view this content.
    So if dblNetWorkDays is zero or negative then you will add on dblEstWorkHrs to the value of dblAvailHours.

    Next, we need to account for the case where dblEstWorkHrs is not enough to bring dblAvailHours out of being negative. In this case you would like to enter an error message into the cell. You'll use another IF...THEN statement to check the condition of dblAvailHours and act accordingly.

    i.e. change your current code snippet from
    Please Login or Register  to view this content.
    to check dblAvailHours before it sets the cell value with:
    Please Login or Register  to view this content.
    So if dblAvailHours is zero or negative then you skip the calculation and insert an error message.


    You also have the option of checking all these values after the user hits the "submit" button. Then if dblAvailHours is zero or below, an 'Abort, Retry, Ignore' messagebox could pop up with the error description which gives the user the option of cancelling or re-entering the submission. I'm sure the forum can help you with that if you wish to implement something similar.

  29. #29
    Forum Contributor
    Join Date
    04-23-2012
    Location
    Bradford PA
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Networkday function in Macro not working

    Quote Originally Posted by Stormin' View Post
    Hi Jared,

    You also have the option of checking all these values after the user hits the "submit" button. Then if dblAvailHours is zero or below, an 'Abort, Retry, Ignore' messagebox could pop up with the error description which gives the user the option of cancelling or re-entering the submission. I'm sure the forum can help you with that if you wish to implement something similar.
    Stormin,

    Thanks again for another wondefully oriented post. I have adjusted accordingly and it seems to work as intended. I greatly appreicate your assistance. I see where you are going with this as technically the user should not be inputting an item that has already exceeded today so it may just be easier to notify them of this and cancel the submission.

    Jared

+ 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. Chaning working hours in NETWORKDAY for fridays
    By Kharr in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 07-10-2013, 06:31 AM
  2. Replies: 6
    Last Post: 05-20-2013, 07:45 PM
  3. [SOLVED] networkday function problem
    By asia abobaker in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 05-07-2013, 09:51 AM
  4. Networkday Formula not working
    By marc5354 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-11-2010, 02:49 PM
  5. Can we use two ranges in Networkday Function?
    By e4excel in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-09-2008, 01:21 PM
  6. [SOLVED] excel "Networkday" function
    By ADS in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-18-2006, 11:00 AM
  7. Businessday (Networkday) Function
    By dannyboy213 in forum Excel General
    Replies: 4
    Last Post: 02-01-2006, 08:35 PM
  8. Replies: 5
    Last Post: 06-16-2005, 06:05 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