+ Reply to Thread
Results 1 to 48 of 48

Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Month

  1. #1
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Month

    I have an Excel 2013 spreadsheet comprising twelve sheets named for the corresponding months starting with Apr (Apr, May, Jun. Jul, Aug etc). With the exception of Apr (the first sheet) each sheet needs to contain the following formula:

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


    It contains two references to data in the previous month (sheet).

    The first reference finds the previous month's name using the formula segment
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    where $B$4 contains the date of the first of the current month. So far this has worked as expected.

    I need to do the same thing for the second occurrence of the sheet name.

    I have tried a straight substitution as follows:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    which is rejected out of hand.

    I have tried various forms of concatenation, wrapping various bits in double or single quotes and other variations, none of which work. They are either rejected completely or give #REF or #VALUE errors.

    This formula, or one similar, is used several times on each of the twelve sheets and manually entering the sheet name is tedious beyond measure and a plentiful source of errors. Once it is finished I will be using the workbook as a template so there is a considerable saving in solving the problem.

    Can anyone help please?
    Attached Files Attached Files
    Last edited by Tegglet; 03-05-2021 at 02:24 PM. Reason: Major error

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,603

    Re: Calculate the Date of a Receipt Based on the Date of the Receipt in the Previous Month

    As your sheet contains nor data nor some examples of expected results ( manual) it's not really easy to help
    As a rule of thumb you will need to wrap the "sheet name" with the INDIRECT function
    Your formula is also a bit weird

  3. #3
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of a Receipt Based on the Date of the Receipt in the Previous Month

    That was quick!!!

    While reviewing the post I found a major ("weird") error. I thought I had uploaded an example spreadsheet, been at it for a few hours must be getting tired!

    I will pick it up again tomorrow, it's now time for dinner, and try your suggestion then.

    Many thanks,
    Tegglet

  4. #4
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of a Receipt Based on the Date of the Receipt in the Previous Month

    Hi Pepe,
    Please see revised post.

  5. #5
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,837

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Assuming that the expected result is "June Target Item 14", try:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    Let us know if you have any questions.
    Consider taking the time to add to the reputation of everybody that has taken the time to respond to your query.

  6. #6
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Hi JeteMc,
    Thanks for that. I have had it working in the example spreadsheet, which is more than I could do before!

    I must admit to be a bit out of my comfort zone with regard to the functions involved (indirect; index etc) but I though I had tried all permutations, obviously not!!

    In the real world it is part of a much more complex formula that is two to three lines long, depending on screen size. It will need inserting in two or three places which will be quite time consuming. I am due to be admitted to hospital later this week and with all the preparation involved' especially with Covid, it is unlikely that I will be able to respond fully until I have been discharged.

    As soon as I am back from hospital I will pick it up and post a proper response.

    In the meantime, many thanks for you help, it is much appreciated.

  7. #7
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,837

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Lord willing, we will still be here when you get ready to pick this back up, so don't worry about that.
    If you have some time while waiting to be discharged you might consider a couple of things.
    1. Would it be feasible to put the result of the formula from post #5 in a cell and let your real world formula reference that cell in the two or three places rather than inserting the formula from post #5 two or three times?
    2. If not, is there a way to post a small desensitized version of your real world workbook, so that we could see if there is a better solution?
    All the best.

  8. #8
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Hi
    I have returned from hospital complete with a brand new hip! The relief is astronomical! :-) The main problem has not been recovering from the hip replacement op but weening off the painkillers taken before it!

    My previous problem was more to do with blurred vision (caused by the powerful painkillers) than anything else, I had a quote mark or bracket in the wrong place which is why copy and past from your solution worked and typing it in didn't!

    Having got the problem as stated solved I now realise I may have been asking the wrong question!

    I am trying to calculate the dates for payments received every four weeks (on Fridays) using an initial seed date on the April (first) worksheet. The formula works fine for the May to December worksheets but gives totally wrong answers for January to March. This year December has two receipts in the same month which is part of the problem which is then compounded by February being exactly four weeks long (I haven't even contemplated leap years yet!).

    For a number of reasons I enter the day of the month (1, 2, 3 etc) in one column and calculate the actual date from that in another column. This means that the different number of days in each month is not handled automatically.

    Is it possible to make the adjustment using a formula created from functions or will I have to resort to VBA?

  9. #9
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,837

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Just returning home, so although post #8 was made almost a week ago, I am just now looking at it.
    I am trying to calculate the dates for payments received every four weeks (on Fridays) using an initial seed date on the April (first) worksheet.
    It seems to me that the following should yield the desired results:
    So that the list of dates start with a Friday, paste the following into cell C4 (April Sheet): =B4+7-WEEKDAY(B4+1)
    In cells C5 and down: =SUM(C4,28)
    For a number of reasons I enter the day of the month (1, 2, 3 etc) in one column and calculate the actual date from that in another column. This means that the different number of days in each month is not handled automatically.
    It would probably help if we could see a sample as I don't really understand what this means.
    Let us know if you have any questions.

  10. #10
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    I have given up trying to do this using formulae, it works alright until there are two receipts in one calendar month. I overcame the problem pro tem by entering the second date manually. I am now trying to do the job properly using VBA.

    To recap:
    I have a complex cash flow forecast spreadsheet in which I want to add entries for four weekly receipts. It comprises one worksheet for each month starting with April (Apr) through to the following March (Mar). For various reasons I enter the day number in one column (C) which acts as a seed for generating the date in the preceding column (B). The sample worksheet attached just has worksheets for April and May; subsequent months are just a repeat of May until there are two receipts in one month. That will be dealt with once I can get this bit working.
    I am trying to write a function to automatically generate the date of the receipt for the current month based that for the previous month. The date for April is entered manually based on the last receipt in the previous financial year.

    My code works as I want it to when I run it from the code window (placing the correct day number in the active cell) but I cannot get it to work by entering “=FindReceiptDate()” or “=FindReceiptDate(parameter)” in the relevant cell.
    I suspect I need to return the value of the “ReceiptDate” variable but cannot find how to do that.
    Can anyone help please?

    The code is as follows (a check for the correct active column (C) will be added later):
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by 6StringJazzer; 09-16-2021 at 03:07 PM. Reason: removed private data from attachment

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Quote Originally Posted by Tegglet View Post
    I have given up trying to do this using formulae, it works alright until there are two receipts in one calendar month. I overcame the problem pro tem by entering the second date manually. I am now trying to do the job properly using VBA.
    Moved from Excel Formulas & Functions
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  12. #12
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Thank you for you friendly and helpful reply NOT! A little toleration in your response would have been nice.

    What do you mean by
    Moved from Excel Formulas & Functions
    ? Where has it been moved to or has it been removed altogether?

    I had major problems navigating the rather convoluted process of entering code and attaching the example file and made a number of attempts at posting my update; losing my content several times. I even posted a query with the webmaster. The forum format and navigation, not to mention some moderators, is not very friendly to occasional users.

    The final post was made by accident and I was so relieved at getting something to stick that actually had retained the attached file that I failed to notice that the code was not enclosed by code tags. Here it is again, enclosed by tags:

    Please Login or Register  to view this content.
    Edit
    Further inspection suggests that I had enclosed the code in code tags. In this post, in which the code definitely was enclosed in tags, the code is displayed in a panel with a grey background and with a "Copy to clipboard" icon identical to my previous post. On what have you based your criticism?
    Last edited by Tegglet; 09-10-2021 at 01:00 PM. Reason: Additional comments

  13. #13
    Forum Guru
    Join Date
    08-28-2014
    Location
    USA
    MS-Off Ver
    Excel 365 version 2501
    Posts
    18,837

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    I asked the moderator to move the thread to a forum on this site where those contributors whom know more about VBA than I (notice at the top of the page it is now in the Excel Programming / VBA / Macros forum) would have a better opportunity to see your post and respond.
    If there is any hostility about moving the thread, please direct it towards me.
    I hope that someone will be able to help and that you have a blessed day.

  14. #14
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Quote Originally Posted by Tegglet View Post
    Thank you for you friendly and helpful reply NOT! A little toleration in your response would have been nice.

    What do you mean by? Where has it been moved to or has it been removed altogether?
    You said you decided you needed a VBA solution, so I moved the whole thread to the VBA subforum, where it will get more attention from people with specific knowledge of VBA. Which I think is actually pretty friendly and helpful. Nothing has been removed. This is not a rule violation. You were not penalized in any way. The thread is still reachable at the same link.

    Edit
    Further inspection suggests that I had enclosed the code in code tags. In this post, in which the code definitely was enclosed in tags, the code is displayed in a panel with a grey background and with a "Copy to clipboard" icon identical to my previous post. On what have you based your criticism?
    Not sure what you are getting at here. I see no comments anywhere in the thread about lacking code tags.

  15. #15
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    To 6StringJazzer.
    My comments regarding code tags referred to the following:
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to
    Please Login or Register  to view this content.
    Regarding moving the post; the message was both unclear and ambiguous, maybe if you had been a bit more explicit, as you have been in your follow up post I would have reacted differently.
    just saying
    Moved from Excel Formulas & Functions
    does not carry the same meaning and implications as
    I moved the whole thread to the VBA subforum, where it will get more attention from people with specific knowledge of VBA
    now that is both friendly and helpful and I thank you for it.
    I was experiencing significant difficulty in navigating the site, especially regarding attaching the sample file, and lost my text several times and had to retype it, perhaps you could use your influence as a moderator on the powers that be to make the process easier to follow. I did send a message using the contact form but I have yet to receive a reply

    To JeteMc
    Thank you for raising the issue with 6StringJazzer. Lets hope it produces results.

  16. #16
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    I have found that people prefer short, to-the-point notes on moderation actions without a long explanation clogging up the thread. It is common to move threads to different subforums to get the appropriate exposure. My apologies if you found it overly terse.

    Quote Originally Posted by Tegglet View Post
    My comments regarding code tags referred to the following:
    That is my signature. It was not aimed at you, or any individual. It appears every time I post to any thread. Anyone can create a signature by editing their profile. My signature first identifies me, then has two reminders. The first reminder is to read the rules because so many people join the board and then start posting without reading the rules. The second is information about code tags, something easy to do but a frequent rule violation.

    I was experiencing significant difficulty in navigating the site, especially regarding attaching the sample file, and lost my text several times and had to retype it, perhaps you could use your influence as a moderator on the powers that be to make the process easier to follow. I did send a message using the contact form but I have yet to receive a reply
    The only problem I am aware of regarding attachments is that the paper clip icon doesn't do anything, so you have to go through different steps. The yellow banner at the top of every page explains how to do this. We are very much open to suggestions about how to make the process easier to follow, so feel free to PM me your ideas.

    The only time I have seen text lost is every once in a while there is a database bug that causes text to disappear if you attempt to edit an existing post that has already been posted. I don't know if this is what you experienced. In such as case when you click Edit Post, the window to edit the text becomes blank. The text is not actually lost; you can hit the Back button to return to the original post without losing anything, but you can't edit it. This is rare. Moderators and Administrators are all volunteers who try to keep the forum running smoothly but we are not employees and no direct leverage with the owners and technical team. All we can is to submit tickets and send requests. I have reported this but it still occurs occasionally.

  17. #17
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    The attached spreadsheet is now current and the previous one should be discarded. The following code matches the attached spreadsheet.

    I have also been getting an intermittent error when the "End Function" statement in the code is ignored and a UDF {PrevSheet()} in a user add-in gets called. This results in an infinite loop which can only be terminated using Task Manager.
    NB: Having checked the file uploaded I see that the UDF in the add-in did not travelled with it. I have therefore entered the date manually.

    All I want to do is call a user defined function from a cell that does the following:
    1) Looks in the previous month for the receipt.
    2) Gets the day the receipt was received.
    3) Adds 28 days to that day then subtracts the number of days in the previous month to get the day the receipt will be received in the current month.
    4) Returns the result and displays it in the cell.
    5) From this the date of the receipt in the current month is obtained.

    I use the day in a separate column as it is easier to manipulate entries when adjusting and sorting entries. It is also less likely to screw up the conditional formatting used in the main spreadsheet.

    It seems to me that the problem lies in how to return the value using the function.

    I would appreciate it if someone would be kind enough to review my code and method of implementation give me some idea of where it is going wrong.

    Please Login or Register  to view this content.
    Last edited by Tegglet; 09-16-2021 at 01:08 PM.

  18. #18
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Quote Originally Posted by Tegglet View Post
    the UDF in the add-in did not travelled with it.
    An add-in is specific to your Excel installation. It is not linked into any of your Excel files. It will work only on your machine, or if other users have the same add-in installed.

    From the notes in your file:
    When I run the function with the parameter "CurrReceiptDate" from the worksheet I get Run-time error " '91': Object variable or With block variable not set".
    The definition of your function does not have a parameter, so you cannot call it with a parameter. Also, to debug UDFs it helps to write a Sub that calls it so you can control the execution better than when it is called from a worksheet.

    I need to spend some time figuring out how your file works but I'll have more input as I go.

  19. #19
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    There are significant problems in the UDF design. As a result it is difficult to determine your intent by reading the code, and the comments do not give a lot of context for what this file is all about. Here is my initial analysis:

    This function is being called from worksheet cells, however, the code is referring to ActiveCell. This is not the cell that contains the call to the function, it is whatever cell is active. Therefore the function will always return the same value in all cells wherever it is used. I doubt that is what you are trying to do, although I still can't figure out what you are trying to do.
    Please Login or Register  to view this content.
    This will always refer to whatever cell the user has currently selected, regardless of what cell contains the formula.

    A UDF called from a worksheet may not cause a change in a cell other than the cell it was called from. Therefore the following line will cause an error 91:
    Please Login or Register  to view this content.
    When I run the function code from the code module (F5) I get the correct result.
    Yes, that is because a function called from a Sub or run by the debugger (F5) does not have the same restrictions as when it is called from a worksheet.

    The return value FindReceiptDate for your function is never set, so it will always return 0 (if it does not produce an error). The line that sets the return value is commented out:
    Please Login or Register  to view this content.
    This code uses Integer variables to store dates. However, Excel dates exceed the Integer value limits. You must use Long for dates.

    It is almost always better to refer directly to sheets and cells. Instead of doing this, this code Selects a sheet and cell, and then depends on ActiveCell to refer to it.
    Please Login or Register  to view this content.
    How does the following line of code determine a date? It refers to column C, which does not contain a date. It contains the result of this function, so it seems this is circular logic.
    Please Login or Register  to view this content.
    Why is the following code commented out?

    Please Login or Register  to view this content.


    I would also restructure the logic to eliminate all GoTo statements. They are not a good programming practice and nearly always unnecessary.

    Once I figure out what you are trying to do functionally, I will provide specific recommendations for revisions.
    Last edited by 6StringJazzer; 09-15-2021 at 11:06 AM. Reason: Added blue text

  20. #20
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    I still can't figure out what this is doing functionally. I get that it somehow tracks receipts but that's as far as I can get. It would help to have more complete sample data. Can you address the following:

    1. Your worksheets only have a few rows of data, with many blank rows in between the populated rows.
    2. Why are so many rows left blank?
    3. What determines what rows are populated?
    4. What determines what data is entered in the rows that do have data?
    5. The only purpose of row 4 seems to be to contain the date for the first day of the month. Is that correct? What does BBF mean? If it is not part of your receipt data I would put it somewhere else on the sheet, although there is no real harm in it.

  21. #21
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Hi 6StringJazzer,
    in answer to your questions:
    1) Your worksheets only have a few rows of data, with many blank rows in between the populated rows. The real life spreadsheet comprises 34 worksheets packed with confidential financial data. I have tried to fillet a couple of sheets so they contain only data germane to the problem.
    2) Why are so many rows left blank? as question 1)
    3) What determines what rows are populated? All rows are populated in sequence and contain the details of the expenditure or receipt for the date in column A.
    4) Column C is the day of the month from which the date in the form dd-mm-yyyy (column B) and the day in the form dddd (column A) are derived. Column E sets the criteria for financial (from drop down list) . Column F contains the description of the item (entered). Columns M and N contain payments and receipts (entered) column O give the status i.e. R for reconciled (entered) and column P is the balance derived from M & N. BBF is Balance Brought Forward (from previous month)
    5) The only purpose of row 4 seems to be to contain the date for the first day of the month. Is that correct? Yes
    What does BBF mean? BBF is Balance Brought Forward (from previous month).

    The code has been much revised and now reads:
    Please Login or Register  to view this content.
    This works on my development workbook when I step though the code using the "F8" key, returning the correct value to the active cell but when I enter =FindCellRef() into the worksheet I get a "#VALUE" error.

    I am not sure how this will work with the example workbook submitted but it will take some time to sanitise a new workbook and it is getting late and well past the time to get my dinner and I am getting VERY hungry.

    I hope the comments help.

    I will pick it up tomorrow morning and create and post a sanitised example workbook.

    Many thanks for your interest.
    Tegglet

  22. #22
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    More answers.

    This code uses Integer variables to store dates. However, Excel dates exceed the Integer value limits. You must use Long for dates.
    The variables "CurrReceiptDate" and "PrevReceiptDate" refer to the day number in the month rather than an actual date and as such are integers, i.e. 21-Apr-2021 would be the integer 21. The actual date is derived from this number. There are a number of reasons for this but it is mainly to facilitate trying out different payment options and dates.

    It is almost always better to refer directly to sheets and cells. Instead of doing this, this code Selects a sheet and cell, and then depends on ActiveCell to refer to it.
    I picked this up from another site and thought it was the way to do it, the code has been updated and now uses a With-End With loop, see latest code.

    How does the following line of code determine a date? It refers to column C, which does not contain a date. It contains the result of this function, so it seems this is circular logic.
    Column "C" is meant to be the day number of the month and is used by the formula in column "B" to calculate the date in the form dd-mm=yyyy and the day in column "A" in the form dddd. See above for an explanation.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    However, you have a point regarding circular logic as this is one of the errors I have been getting. I need the function to return the day number of the receipt for the current month, so how do I do that without creating a circular reference?

    Why is the following code commented out?
    This code is meant to deal with the month that has two receipts in it .i.e. if there are more than 28 days from the receipt date to the last day of the month. The month will vary from year to year. It will be implemented once the main code is working properly and may be ignored for now.

    The crux of the matter, as I see it, is how to pass back the value obtained by the function. With the code segment
    Please Login or Register  to view this content.
    activated, running the code from the code window using F5 gives the correct result and running
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    gives a #VALUE Error.

    I am trying to get the code into the example worksheet already posted and am now getting some quite bizarre results!
    The code line
    Please Login or Register  to view this content.
    is going to a totally unrelated function in a different module that I use to obtain the value of the corresponding cell in the previous sheet. Running the code again it stops at
    Please Login or Register  to view this content.
    and displays a Run-time error '1004' Unable to get the EoMonth property of the worksheet function class. It displays it for the next line of code as well. Closing the workbook and reopening it lets this code fragment run.

    I am now totally confused and not sure what to do next so I am taking a break and will come back to it with, I hope, a fresh pair of eyes and a clear head.
    I hope the answers to your questions help.
    Thanking you once again for you interest
    Tegglet

  23. #23
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    I am in the weeds on your code without understanding the actual application you are trying to develop. I read your description and still cannot relate the bare-bones file with what you are actually doing. The fact there are only two sheets is fine, but I just don't understand how your data works. If I can understand that, I expect I will start fresh instead of trying to modify existing code.

    1) Your worksheets only have a few rows of data, with many blank rows in between the populated rows.

    The real life spreadsheet comprises 34 worksheets packed with confidential financial data. I have tried to fillet a couple of sheets so they contain only data germane to the problem.


    2) Why are so many rows left blank?

    as question 1)

    There is nothing here that explains why you just have a few populated rows scattered about and mostly blank rows.

    3) What determines what rows are populated?

    All rows are populated in sequence and contain the details of the expenditure or receipt for the date in column A.

    If they are populated in sequence, it does not explain why only a few rows are populated with blank rows in between.

    4) Column C is the day of the month from which the date in the form dd-mm-yyyy (column B) and the day in the form dddd (column A) are derived.

    This is probably the point where my understanding falters, and is also what your question is all about. Why do you need a function to calculate this day? Why not just enter the actual day that the actual transaction occurs?

    Column E sets the criteria for financial (from drop down list) . Column F contains the description of the item (entered). Columns M and N contain payments and receipts (entered) column O give the status i.e. R for reconciled (entered) and column P is the balance derived from M & N. BBF is Balance Brought Forward (from previous month)

    5) The only purpose of row 4 seems to be to contain the date for the first day of the month. Is that correct?

    Yes

    What does BBF mean?

    BBF is Balance Brought Forward (from previous month).

  24. #24
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Hi,
    Why do you need a function to calculate this day?
    There are a multitude of reasons, not the least of which is it removes the risk of error which will prevent major inaccuracies in forecasting cash flow. The function will be included in the workbook template and so automatically populate the required row for each year. This will be a major timesaver when setting up the new financial year. Without sight of the real life workbook which contains a shedload of confidential information the full explanation would be way too long and convoluted for the forum. Please accept it as a given that this is what I consider the best way to deal with a number of situations.

    The function (“PrevSheet()”) is heavily used throughout the real life workbook and is normally held in a workbook loaded as a user created add-in. This did not travel with the original example but seems to figure in the problem for some reason. I have therefore moved it to a separate module within the new example workbook where it exhibits the same anomalous behaviour.

    It is used with a cell reference passed as a parameter. It gets the value of the quoted cell from the previous sheet and displays it or uses it in the current sheet. For example PrevSheet(M50) gets the value in the cell M50 on the previous sheet and displays it in the calling cell on the current sheet; see cell N4 on the May sheet.

    The code for PrevSheet() was lifted of the web many years ago and tweaked to suit. It does not work with Excel 2016 however, producing circular references for some reason. I did put it up on the forum but got no response.

    The link for that post is #N/A” Error Encountered When Copying Formulas in O365 Excel 2016 (excelforum.com)

    I finally solved the problem by reverting to Excel 2013. I came to the conclusion PrevSheet() either exploits a bug in Excel 2013 or finds one in Excel 2016, I have not tried it in Excel 2019. It now looks as if it has come back to bite me in the bum since it seems to be part of this problem.

    The function in question, now inappropriately, named FindCellRef() is supposed to look in the previous sheet for the value of the cell in column C that corresponds to the row containing the record for the receipt and from that calculate the date of the receipt for the current month.

    In the example workbook this is the value in Cell C13 (23) on the Apr worksheet. It then adds 28 to that value and finally subtracts the number of days in the previous month to obtain the day for the current month. It should then display the value in the calling cell. In the example workbook this should be the value 21 on the May worksheet.

    For some obscure reason when it executes line 72 it calls the PrevSheet function. See comment also on line 73. NB: In writing up the problem and verifying it I did find this behaviour inconsistent, as was the notification for a circular reference.

    The following formula performs the above task correctly provided there is only one receipt due for the month in which it is called, it cannot, however, cope with two receipts in one month, which happens on one month of the year. Maybe it will help you tease out what is required of my function FindCellRef().

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    it is called from cell C15 on the May worksheet,

    I could not find a way of modifying it to give a correct result when there were two receipts on one month. The formula just got way too complicated which is why I tried using VBA.

    As a list of instructions this is what is required:
    1. Establish the current sheet (Line 42).
    2. Establish the number of days in the previous month (Line 44).
    3. Check that it is not the first sheet in the workbook (line 51).
    4. Activate the previous worksheet (Line 59). I have activated the worksheet rather than selected it as I read somewhere that “.Find” only works on the active worksheet.
    5. Search the cells in column F until it finds one containing the search string, in this case “Receipt” (Line 61).
    6. Check that the search string has been found (Line 62).
    7. Obtain the cell address for the search string (Line 64).
    8. Obtain the value of the corresponding cell in column C (Line 66). This is the day that the receipt was received in the previous month as an integer.
    9. Activate current worksheet (the one we started with) (Line 70).
    10. Calculate the day the receipt will be received in the current month (Line 71).
    11. Set the value of C24 (arbitrary choice, could be any cell in Column C) to the current receipt date Line 73). This is a debug line only and is not required once the function is working. It does, however, cause an unpredictable action in jumping to the PrevSheet() function which can cause all sorts of upset! It may or may not be relevant.
    12. Attempt to set the return value of FindCellRef to the current receipt date (Line 75).

    Note: Code for detecting two receipts in one month is yet to be attempted.

    I hope this helps, if it doesn’t then I am at a loss!

  25. #25
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    There should be a file attached
    Attached Files Attached Files

  26. #26
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    The code for PrevSheet() was lifted of the web many years ago and tweaked to suit. It does not work with Excel 2016 however, producing circular references for some reason. I did put it up on the forum but got no response.
    It works fine for me in Excel 365 in your current file and also the file you posted in the other thread. The code is pretty straightforward and I don't see anything that would cause this. Not being a professional Excel consultant, I do not have any older versions of Excel to test with.

    Please Login or Register  to view this content.
    Range is not a data type nor keyword, it is a built-in class (Workbook and Worksheet are examples of the same thing). Keywords will be shown in blue (by default--you can change it).

    Please Login or Register  to view this content.
    I strongly discourage using GoTo like this. Instead, the logic you want to skip should be included in the If part above. GoTo has been considered undesirable by professional software developers for at least 50 years. This is the source of what we used to call "spaghetti code." There are very special situations where I would not object but this isn't one of them.
    Please Login or Register  to view this content.
    Similar comment here.

    Please Login or Register  to view this content.
    I explained this sort of thing in an earlier post. A Function that is called from a worksheet formula has one and only one purpose: To return a value. You may not use a function called from a worksheet to make any changes to any other cells. These are called side effects. That is why you get a #VALUE error.

    Let me tell you how I would do this, now that I get a better picture of what your data looks like. I would use VBA to create each new Month sheet. I would create a hidden sheet that is the template for this month sheet, so the formatting (including conditional formatting) and fixed text is already in place. The VBA would set up the blank month sheet from the template, bringing the prior month's balance forward, and then prepopulate the events where you get the receipt every four weeks. To do this, it would need some anchor date where the first receipt occurred. I suppose it could retrieve this from a prior sheet. Then after you enter your daily data, you would re-sort to your heart's desire.

    The one thing that looks like a problem to me is that on April 27 you have a paid amount of 52.00 but the Item is "Receipt". This entry will be found by your existing VBA code, because it searches for the word "Receipt" using xlPart as the search option. It would be better to search for the entire phrase "Receipt - Every four weeks" and use xlWhole.

    For the sake of closure, here is how I would rewrite the code to eliminate the GoTo statements.

    Please Login or Register  to view this content.

  27. #27
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Here is a new solution.

    I have added two sheets:

    • Controls: Has one button to create the next month's sheet. Can be updated to include other buttons if needed, or this button could be moved somewhere else.
    • Template: A template for a month sheet that has all formulas and formatting. This is a hidden sheet but could be visible if desired.


    The button to add a sheet does this:

    1. Copies the template to create a new sheet after the latest month
    2. Renames the new sheet based on the month in the form Mmm
    3. Finds a recurring receipt in the previous month and retrieves the date
    4. Increments that date by 28 days and finds all occurrences in the new month (1 or 2 occurrences)

    You will then key in your other transactions manually as needed, and sort as desired.
    Attached Files Attached Files

  28. #28
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Hi,
    Whilst I appreciate your suggestions and am grateful for the time you are spending on my problem we do seem to be suffering a bit from mission creep. Your solution would mean an extensive rewrite of the real life worksheet using VBA and I am not sure my VBA skills are up to that yet! :-(

    There is a lot more to the real life workbook than the example put up on the post. The example has been cut down in an attempt to comply with Forum rules. The full workbook has a total of 35 sheets with many interdependences and sanitising it would be both very time consuming and run a high risk of missing sensitive data. I am sure it would break the rules for simplicity and brevity.

    In addition there are a number of other entries with different intervals, descriptions, and amounts for both receipts and expenditure and I was hoping to extend the scope of the UDF to include these once I had got this one working.

    I do now understand that a function, whether built in or a UDF, cannot change a cell other than the cell from which it is called, which I did not when I embarked on this task. I cannot, however, understand why a UDF cannot find a value on another worksheet on which to base a calculation to obtain its return value? To me it seems an unreasonable restriction, after all my public UDF "PrevSheet(cell ref)" does just that. I have tried running PrevSheet() from within VBA, which I thought was legal, but I get a compile error “Sub or Variable not defined”.

    The formula in Cell C15 on the May worksheet does what I want the UDF to do but I cannot get it to cope with two receipts in one month. Is there not a way to do what that formula does using VBA? I have tried “Record Macro” then running the formula but all I get in the macro is "Range("C16").Select"

    If I cannot do this with a UDF then I will have to give it up as a bad job as I am not I a position to start the whole thing from scratch. I seems it’s going to have to go on the “too hard heap” unfortunately.

    Maybe after studying John’s book I will get some other ideas or acquire the skills to follow up on your suggestions.

    Thank you for your kind efforts, they are much appreciated.

    Regards,
    Tegglet

  29. #29
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    I cannot, however, understand why a UDF cannot find a value on another worksheet on which to base a calculation to obtain its return value? To me it seems an unreasonable restriction, after all my public UDF "PrevSheet(cell ref)" does just that. I have tried running PrevSheet() from within VBA, which I thought was legal, but I get a compile error “Sub or Variable not defined”.
    I did not know this was not representative of your actual file or I would not have invested the time in a redesign.

    I don't recall this particular problem and a UDF should definitely be able to do this. What exactly are you doing to call PrevSheet that is giving you an error?

  30. #30
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Hi,
    Sorry for not making it clear that there was a lot more to it. I thought that if I posted the whole thing with a full description I would be breaking Forum rules, or at least bending them out of shape!

    In recapping I noticed once again your emphatic comments regarding GoTo statements.
    I strongly discourage using GoTo like this. Instead, the logic you want to skip should be included in the If part above. GoTo has been considered undesirable by professional software developers for at least 50 years. This is the source of what we used to call "spaghetti code." There are very special situations where I would not object but this isn't one of them.
    When I first became involved with writing code (believe it or not that really was about 50 years ago!) it was drummed into me that I should not exit a routine from the middle and any conditional statement checking for a terminating condition should go to the end of the routine if one was found. This is the only time I use GoTo statements as I agree with you that using them in other circumstances leads to code that is more like a maze or as you put it spaghetti. In my support the statement "On Error GoTo ErrHandler" seems to be used extensively and seems to me to use a GoTo statement in much the same way as I am. Given that I ONLY use them in these circumstances can we agree to differ on this topic?

    I have been trying to find ways round the problem but hit a brick wall every time and it has been most frustrating; although I am broadening my VBA knowledge considerably.

    The first issue was with the following code lines:
    Please Login or Register  to view this content.
    The first line seems to be ignored so the second line (SearchValue ) only looks in the current sheet i.e. the one from which the UDF is called and of course it returns "Nothing" resulting in CurrReceiptDate having the wrong value. I have tried both activating the sheet and selecting the sheet, neither seem to work. This code segment works perfectly when used in a sub.

    Getting this to work would be the ideal solution.

    Regarding the calling the PrevSheet() function I have deleted to workbook in which I was trying to use it and I cannot reproduce the problem at the moment but as far as I remember I had to use Application.WorksheetFunction.PrevSheet(CellRef) or similar.

    The other problem was getting it to recognise the argument. I have tried several ways of entering the cell reference including one set of double quotes and two sets of double quotes all to no avail. I am afraid this is showing up my sketchy knowledge of VBA. The code segment can be inserted in the example workbook uploaded already (I created a separate module “CheckPrevSheet” in my copy for clarity).

    All this function is required to do is get the value of the Cell reference used as an argument from the previous sheet as it does when called from a worksheet. It is not part of the original problem.
    Please Login or Register  to view this content.
    The plan was to call PrevSheet from within a loop indexing the PrevSheet argument from $F5 to $F49. This was a dismal failure and I am not spending any more time on it although it would be useful to know how to enter the cell reference for future use.

    I had more or less given up on finding a simple solution that can be used with the existing workbook and I have been following up on your suggestion of recreating the entire workbook from scratch using VBA. I don’t want it written for me as part of the reason for the exercise is to keep my brain active and it is certainly doing that! This I am treating as a long term project to be ready for next April. I am hoping John’s book will provide the help I will be needing.

    I have been trying out various bits of code before trying to create a new workbook and it has been refreshing trying out things that act in a predictable manner! The most interesting thing so far is setting up the conditional formatting using VBA. Conditional formatting has been a thorn in my side for a number of years and two of the supernumerary worksheets are copies used to transfer the formats across when the conditional formatting creates numerous segmented ranges. A button to reset these ranges will be both quick and more reliable.

    Thanking you once again for your continued interest and support.
    Tegglet

  31. #31
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Quote Originally Posted by Tegglet View Post
    Given that I ONLY use [GoTo statements] in these circumstances can we agree to differ on this topic?
    Of course, you are free to do this in whatever you think is the best way. I was a professional software engineer and engineering manager for 44 years and wrote production code in FORTRAN, PL/1, Ada, JOVIAL, C, C++, and Java, and I have never written a single GoTo statement in production code. Just my 2¢.

    The first issue was with the following code lines:
    Please Login or Register  to view this content.
    The first line seems to be ignored so the second line (SearchValue ) only looks in the current sheet i.e. the one from which the UDF is called and of course it returns "Nothing" resulting in CurrReceiptDate having the wrong value. I have tried both activating the sheet and selecting the sheet, neither seem to work. This code segment works perfectly when used in a sub.
    In VBA, there are two best practices for dealing with sheet references. These are my opinions, but the opinions of a professional programmer who has been using VBA for 17 years.

    1. Write code in the module for the worksheet being referenced. All unqualified references (such as Range("F4:F49")) will default to that worksheet
    2. If you are writing code in a standard module (like Module1) that is intended to reference specific sheets, then you should explicitly qualify all sheet references, rather than using Activate then expecting references to default to the active sheet. The only exception to this is if you are writing a general macro to be called from a button that always operates on whatever sheet is currently active as a result of user actions.

    Therefore, change the above two lines to this one line to eliminate any uncertainty regarding defaults:
    Please Login or Register  to view this content.
    I will wade through the rest of your post when more time makes itself available.

  32. #32
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Hi,
    Thanks for getting back so quickly.

    Acting on your recommendation I have changed the function to the following:
    Please Login or Register  to view this content.
    I have added "Worksheets(PrevWorksheet)" to the line that gets the PrevReceiptDate.

    This works for the May sheet, calculating the correct date, but when I create the Jun sheet (by copying and renaming the May sheet) I get a circular reference. This I have encountered before but cannot see why it creates a circular reference for the Jun sheet but not for the May sheet. Am I being a bit thick here?

  33. #33
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    If you are creating the Jun sheet with a manual copy, I would have to see the result to determine why it creates a circular reference. Do you see what cells are involved in the circular reference?

  34. #34
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    The errant cell seems to be C15 on the May sheet which displays the correct result from the function, No circular reference arrows are displayed. I have attached the Workbook with additional sheets.

    To reproduce the circular reference in the new workbook do the following:
    1) From the May sheet select the cell containing the function and place the curser in the formula bar.
    2) Invoke the function by pressing return. This will calculate the date for May correctly using the hard value in Apr!C13 as a seed.
    3) Move to the Jun sheet and do the same. In this case we are looking at the result of the UDF in May!C15 rather than a hard value. This should result in the circular reference error message being displayed.
    4) Clearing the message leaves a zero (0) value displayed in the cell containing the UDF.
    5) Inspecting the Error Checking drop down > Circular references from the Formulas ribbon shows the cell reference May!$C$15 as the one containing the circular reference.

    It would seem that we need the value displayed in the cell rather than the contents, a bit like copying a cell containing, say =1+2+3 then using Paste Special>Paste Values123 to get the cell to display 6. I am not sure how to do this in VBA, or indeed if it can be done.
    Attached Files Attached Files

  35. #35
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Looks to me like the circular reference is because cell B4 is calling function PrevSheet(B4). This is the definition of a circular reference. Excel doesn't know your UDF is using the address, not the value, of B4.

    Also PrevSheet is not in this file so I am getting a NAME error.

  36. #36
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Sorry about that, I have it loaded as an add-in so I have to take it out when running on my machine, I forgot to put it in when I uploaded the file.

    If you replace the commented out code in module 1 with the following it should work.
    Please Login or Register  to view this content.

  37. #37
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    I didn't need that to diagnose the circular reference. In your case the circular reference is benign. You can ignore it, but you can't get rid of it unless you redesign how PrevSheet works. One option would be:

    Please Login or Register  to view this content.
    This function will always return the data in the corresponding cell from which it is called. That seems to be how you intend to use it anyway. Now you don't need to include the cell as an argument.

  38. #38
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    That returns a #VALUE error! I have to confess the code was lifted of the Internet and worked so I have spent very little effort in finding out how it works; maybe I should!

    The lines "Dim rCell As Range" and "Set rCell = Application.Caller" are shown in red in your example; is that significant or just to bring my attention to the differences?

    Regarding the argument not being required, how does it know which cell on the previous sheet contains the required value? For instance when used to get the previous months balance, the UDF entered in cell M4 needs the value in cell N50 on the previous sheet and the UDF in cell N4 needs the value in cell M50 on the previous sheet.

    How and why does the PrevSheet UDF affect the FindCell UDF?

    I have had problems in the past with other iterations of FindCell() not terminating but for some reason going on to run PrevSheet instead. As far as I can remember it did not respond to "On Error GoTo errHandler" either. The last time this happened was when I had a code line that tried to change a cell other than the calling cell. Whilst I now know that is not allowed, shouldn't VBA deal with what is essentially an illegal call more elegantly?

  39. #39
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Quite by accident I have just found someone also having problems with the PrevSheet UDF, not only that they had a solution that works! It is at
    HTML Code: 
    (not sure if I got that right). Maybe my computer took pity on me and went looking on its own! ;-)

    the code is:
    Please Login or Register  to view this content.
    I have done a quick check and it seems to work as is. I just hope there is nothing lurking in some dark dank corner waiting to pop up and bite me in the bum!

  40. #40
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,306

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Thanks for sharing.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  41. #41
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,306

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Just followed your link - what a coincidence that that 'someone' has coined the exact same user name as you!!!

    Was this the link you meant to share???

    https://answers.microsoft.com/en-us/...e-9f224784e7a1

  42. #42
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Thanks for sharing.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of all those who offered help.
    I will be checking it this weekend. I have just revisited the page with the PrevSheet UDF and upon scrolling down found that I had responded in 2018 when I found that it did not recalculate automatically, and it still does not. I did not get any response then and it appears that it got put on the too hard heap!

  43. #43
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Just followed your link - what a coincidence that that 'someone' has coined the exact same user name as you!!!

    Was this the link you meant to share???

    https://answers.microsoft.com/en-us/...e-9f224784e7a1
    That was me!!! Our posts seem to have crossed. See my previous post.

  44. #44
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,306

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Sorry - I am not following you at all. You said this:

    Quite by accident I have just found someone also having problems with the PrevSheet UDF, not only that they had a solution that works! It is at
    HTML Code: 
    (not sure if I got that right). Maybe my computer took pity on me and went looking on its own! ;-)
    But the link goes to one of YOUR threads on another forum.

    So where is the thread by the 'someone' who had the working solution?
    Last edited by AliGW; 10-02-2021 at 06:20 AM.

  45. #45
    Registered User
    Join Date
    12-21-2017
    Location
    Wokingham, England
    MS-Off Ver
    office 365 2013
    Posts
    46

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    When I first saw the post I did not notice it was one of mine from 2018, I just looked at the code and thought someone else had had the same problem, didn't realise it was me until I revisited the page and scrolled down!! Yet another senior moment!!

    I has given me food for thought though.

  46. #46
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,306

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    OK, look, here's what I'd suggest: post the code for the UDF here (with a nod to Gord Dibben, who offered it to you). VBA experts here may be able to help you to tweak it to work as you wish it to. No need to start a new thread - just add it here.

  47. #47
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    Quote Originally Posted by Tegglet View Post
    That returns a #VALUE error!
    Sorry, the order of statements needs to change:
    Please Login or Register  to view this content.
    The lines "Dim rCell As Range" and "Set rCell = Application.Caller" are shown in red in your example; is that significant or just to bring my attention to the differences?
    Only to show what I changed.

    Regarding the argument not being required, how does it know which cell on the previous sheet contains the required value? For instance when used to get the previous months balance, the UDF entered in cell M4 needs the value in cell N50 on the previous sheet and the UDF in cell N4 needs the value in cell M50 on the previous sheet.
    I was just trying to resolve the circular reference issue and focused on B4. I did not notice what was going on in those other cells. This version of the function assumes that if you call it from B4, you want B4 on the other sheet, and so on. If you call it from M4 but need to reference N50, then this will not work for you. Another option is to have it return the sheet name, then use an INDIRECT formula to reference the desired cell.

  48. #48
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,715

    Re: Calculate the Date of an Item Based on the Date of a Similar Item in the Previous Mont

    To wit:
    Please Login or Register  to view this content.
    And to use:

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


    Avoids circular reference, and you can insert any cell reference desired in place of B4.

+ 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. [SOLVED] Index match date, receipt number, invoice number and amount from 3 worksheets
    By sunboy in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-16-2020, 12:16 PM
  2. Formula To Calculate Month in a Previous Date
    By breader21 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-07-2020, 10:44 PM
  3. Replies: 13
    Last Post: 11-22-2019, 08:27 PM
  4. [SOLVED] Actual Receipt Date Vs. Original Receipt Date (Compare & Provide Result)
    By Rajeshkumar R in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-05-2018, 07:06 AM
  5. Replies: 5
    Last Post: 02-06-2013, 02:11 PM
  6. Date of Receipt based on In/OUT
    By cmcconna in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2010, 06:47 PM
  7. Auto calculate end date of previous month
    By BusterBoy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-28-2009, 07:57 PM

Tags for this Thread

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