+ Reply to Thread
Results 1 to 22 of 22

subtracting dates results in a big number when a date field is blank

  1. #1
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    subtracting dates results in a big number when a date field is blank

    Hello all,

    How can I change the

    Please Login or Register  to view this content.
    formula in the "Number of Days" column to simply put a "-" (as text, not a number) for any cell in the "Number of Days" column whenever there's no date in the "End Date" column?

    Please Login or Register  to view this content.
    Currently, the code brings up numbers like "-39840"

    2. How can I stop this formula from calculating once there's no more data in the "Start Date" column?

    thanks much
    Last edited by duugg; 07-23-2009 at 10:33 AM. Reason: searchability

  2. #2
    Forum Expert oldchippy's Avatar
    Join Date
    02-14-2005
    Location
    Worcester, UK
    MS-Off Ver
    Excel 2007 (Home)
    Posts
    7,097

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Hi,

    Change the formula in column C to

    =IF(B2="","",B2-A2) then copy it down
    oldchippy
    -------------


    Blessed are those who can give without remembering and take without forgetting

    If you are happy with the help you have received, please click the <--- STAR icon on the left - Thanks.

    Click here >>> Top Excel links for beginners to Experts

    Forum Rules >>>Please don't forget to read these

  3. #3
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Thanks but the only problem is that I want to automate this via code because I never know what the last row of data will be in the "Start Date" column.

    Do you know the code equivalent for that formula plus to have it automatically stop at the last row of data in the "Start Date" cell?

    thanks,

    duugg

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Please Login or Register  to view this content.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  5. #5
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    mudraker,

    Great! This works!

    2 hopefully quick things...

    1. Can we change the code to look for the 3 columns by name rather than by column number? These columns will never be in a fixed position.

    2. Can we copy, paste special, values only - the "Number of Days" column after the formula has been run?

    Once I get the values, I'll need to do further things to the "Number of Days" column using just the values only, so getting rid of the formula "post value" will be a big help.

    Thanks much

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Do you mean a column header

    Can we change the code to look for the 3 columns by name rather than by column number? These columns will never be in a fixed position
    If you do, it is possible - I did a code for something similar a few weeks ago
    Will the headers always be in the same row e.g row 1

  7. #7
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Yes, the three column headers are always in row 1 but never in the same column position.

    I believe you helped me out with a similiar one before, so that might've been me

  8. #8
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    bump, no response

  9. #9
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Minusing one date from another results in "-39856" when a date field is blank

    The code I provided previously shows how to find the column with the title you need:
    Please Login or Register  to view this content.
    All those lines do is go through each cell of row 1 and compare the value in the cell to the string you are searching for; namely, the "date opened".

    You must note that if the string is not found, then the control counter ctrl is incremented by one to the next column.

    When the string is found DateCol is set to the column number of the cell where the string was found and the loop terminates.

    I used 256 in the loop only because xl2000 sheets only have 256 columns.

    A better way (I didn't think of at the time) is as follows:
    Please Login or Register  to view this content.
    Ben Van Johnson

  10. #10
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Hi protonleah,

    I'm having a bit of trouble interpreting this code. I tried putting the code in between Sub and End Sub and nothing happened. Is there some line I should change to accommodate for the "date closed" column? I don't see mention of it in the last piece of code.

    Sorry to be such a newbie

    thanks

  11. #11
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Hi,
    Previously you said:

    1. Can we change the code to look for the 3 columns by name rather than by column number? These columns will never be in a fixed position.
    The code that I posted was only meant to show how you can capture the column number by comparing the data in the cell with the title you are looking for.

    Since you said that you don't know which column the titles will be in, you will have to look at each cell in row one.

    The line:
    Set HeaderRow = Range("1:1")
    essentially, loads a list of cell addresses into the variable HeaderRow. Range("1:1") says ALL cells in row 1 [it's equal to: Range("A1:IV1")].

    The code:
    Please Login or Register  to view this content.
    is just a loop:
    1. TestHeader is loaded with the info in the first cell of the list contained in HeaderRow.
    2. This info/data is compared with the title you are looking for (held in Hdr2Find).
    3. If the two match, then HdrColumn is loaded with the column number
    This is the whole purpose of the loop. You now have the column number you need. In this case it finds "start date". You need to use this in the code that was provided by MudRaker and OldChippy.

    For example, if the start date was in cell G1, then after the code is run, HdrColumn will contain 7 since column G is the seventh column. So, instead of using "Range(G1"), you would use Cells(1,7).

    Since you want to search for the column number of three different titles, you will need to have three blocks of code at the top of your macro:
    Please Login or Register  to view this content.
    At this point you will have three variables, namely, HdrColumn1, HdrColumn2 & HdrColumn3 locating the data you need. But, you will need to use them elsewhere in your macro...

    Q1: When you run your code in VBE do you have the Immediate Window and the Watches window open?
    Q2: Do You know how to use Debug.Print commands?

  12. #12
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Hi protonleah,

    Very interesting stuff indeed! I LOVE when you pros explain things in Laymen's terms. I will def check into all this code soon enough. I love the explanation of each line of code. I will def learn a lot from it.

    Answers to your questions


    Q1 - When you run your code in VBE do you have the Immediate Window and the Watches window open?

    Answer - Hmm, I didn't know I could "watch" the macro from the "VBA" side, if that's what you mean? What's the advantage in doing so?


    Q2: Do You know how to use Debug.Print commands?

    Answer - Don't even know what that is, sorry


    Thanks again, I'll will def analyize this new code and concept

  13. #13
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Minusing one date from another results in "-39856" when a date field is blank

    The Immediate and Watch windows (View menu) are a couple of debugging tools. For instance, given the code loop below:
    Please Login or Register  to view this content.
    1. Set the cursor on the variable K, right click the mouse and select "Add Watch", then do the same for L and R. Hi-lite "R.Value" and add another watch.

    Press F8 to step through the code one line at a time. Notice in the Watch Window that as the variables change, the results are shown in the Watch window.

    Also notice that the variable R has a "+" sign to the left. Click it and you will see many, many properties that are available to range variables and most can be accessed with VBA code.

    In the Immediate window, notice that every time the debug.Print line is executed it displays the current info stored in cell A1. You could have had a line such as: Debug.Print k;L;R.Value (it would print all values on the same line, though). To print each variable on its own line would require separate Debug.Print lines for each desired variable.

    Also very useful is the setting of BreakPoints (see Help)

  14. #14
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    protonleah,

    You reminded me of a good question I had regarding this. I wanted to step through this code:

    Please Login or Register  to view this content.
    But it checks like 2,000 rows. Can I go through just this macro automatically and then stop right after it's done executing so that I can step through the rest of the code manually?

    thanks for this and thanks for all that info above. I will look at it probably tomorrow at work. Looks really cool

  15. #15
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Option 1
    Place the command Stop at the point you want the code to stop for you - this puts you into dubug mode

    Option 2
    Select or place cursur in the command you want to go into debug mode on and press F9
    - F9 toggles breakpoint

    Option 3
    Click in the narrow border to the left of you command - this does the same as option 2

  16. #16
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Hi protonleah,

    Watch Window
    I spent about an hour and a half playing around with the "watch" window and, while I do see what's happening, I def don't know enough of VBA yet to know how this can help me. Great future reference though and I do thank you for taking the time to write that part out.


    The "TestHeader" loop

    This part is very interesting to me as almost all my macros seem to require at least 2 things...

    1. Macro must be found by column header (name)
    2. Column header is always in first row


    You wrote the code here to find the column header by name...

    Please Login or Register  to view this content.

    I wanted to do a test to see how the macro recorder version of this would be to put the text "Water" in the column header named "Drink" and this is what I came up with...

    Please Login or Register  to view this content.

    I know for a fact that your code MUST be better but don't yet understand why.
    To make this simple so that I can understand the difference between "macro recorder code" and "protonleah code", if you wouldn't mind, would you show the same result using your code to put the word "Water" in the second row of the column header named "Drink"?

    I'll be very interested to see how this looks...thanks much!

  17. #17
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Below is a most basic answer:
    NOTE: I had to change the parameter LookIn:=xlFormulas to xlValues to get your macro to run.

    1. When you use the macro recorder, it records every key press and scroll adjustment (no matter how small). Recorded macros that don't do very much get very big, very fast and they are very hard to read and make heads or tails of.
    2. In your code, you have XL search row one for the word drink, but if that word is not found, then the macro crashes, using VB you can test for success or failure.
    3. The line after the search/find line selects cell F2. Anyone reading this would not know for sure if the word "drink" was found in cell F1 or not. XL simply recorded that you clicked F2 after the find. You could have clicked on any cell and typed in "water". For instance, with "drink" in N1:

    Please Login or Register  to view this content.
    Here, I clicked G28 and typed "water" and pressed enter, which moved the selection right one cell. Clearly, G28 and H20 had nothing to do with "Drink" in N1.

    Most importantly, however, you must still edit the macro in order to capture the address of the found item for use later, in say a loop of some kind. I.e., you will still have to Dim some variables and enter statements like:

    var1= ActiveCell.Address(0, 0)
    or, var2 = ActiveCell.Column
    or, var3 = ActiveCell.Row

    But, for such a small macro the difference between the recorder and VB isn't much

    Please Login or Register  to view this content.
    You can replace the 2 (the row number) in the above line with some loop counter to move up/down in the column.
    Also, you don't have to select the cell in order to write a value to it. The only way to get the macro recorder to enter a value in a cell is to click the cell and type the value since you are working directly in the worksheet.

  18. #18
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: Minusing one date from another results in "-39856" when a date field is blank

    Ahh,

    Very interesting information. Thanks for the "column find 101". I hadn't thought of what would happen if the column weren't found, which would create an error. Good information to know and use. I will try and implement that into future subs if possible.

    Thanks protonLeah!

  19. #19
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Post Re: subtracting dates results in a big number when a date field is blank

    Incidently if you have a lot of columns, then using .Find as the recorder did is generally quicker than cycling through each cell checking for a matching value.

    The correct form of the .Find version would be as below:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    If any of the HeaderCol variables are 0 then it means they weren't found.
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  20. #20
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: subtracting dates results in a big number when a date field is blank

    I will keep this in mind

    Thanks Phil_V !

  21. #21
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: subtracting dates results in a big number when a date field is blank

    No problem, you mentioned earlier that you often need to find a column by it's header text, so the function may be useful to you in the future

  22. #22
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: subtracting dates results in a big number when a date field is blank

    Indeed, this could def be a help on future macros.


+ 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