+ Reply to Thread
Results 1 to 18 of 18

copy cells from sheets based on date, paste to another sheet

  1. #1
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    copy cells from sheets based on date, paste to another sheet

    I am getting myself in a knot here.
    On sheet "CoA" i have at table of periodic payments that is created via an input form and what I want to do is have accounting entries automatically generated in my cask book based on dates.

    The code for the input form is as follows;
    Please Login or Register  to view this content.
    The table entries work fine but the entries on sheet "Receipts & Payments" don't get made. It seems I am not using the date logic properly.

    Then I have problems getting the change event / date based entries to work.

    I think the logic is evident from the code and notes;

    Please Login or Register  to view this content.
    Can anyone see where I am going wrong? Please?
    Attached Images Attached Images

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    I am not sure if this will solve your problems, but I would assign Today = Date, not Now. Date is an integer for today, Now is a decimal. The way your code is written I believe Today would always be > txbDate.value, unless that value is tomorrow or later. Also, if txbDate is a text box, you may want to go CDate(txbDate.value) to turn the string into a date value.

    Does that help?
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    Thanks for your help. It is getting a bit late here and my wife has just arrived home so attention is needed elsewhere. I will try your suggestions tomorrow.

  4. #4
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    Quote Originally Posted by davegugg View Post
    ...Today would always be > txbDate.value, unless that value is tomorrow or later. ...
    Although true for most cases it is possible for the user to set up the schedule for future commitments.

  5. #5
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    Quote Originally Posted by upod View Post
    Although true for most cases it is possible for the user to set up the schedule for future commitments.

    I just want to be sure you understand what happens when the current date is put into cbxDate.

  6. #6
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    Well I am not sure the suggestions are helping (tbxDate, not txbDate) but I am getting a 1004 error "method select of object range failed" pointing at Range("I47").Select, the fifth line of code on the worksheet change

    At the risk of repeating myself there are 2 distinct events; the first when the user sets up a schedule of future payment commitments, and the second based on a worksheet change. Because the first event makes an entry on the Receipts and Payments worksheet it triggers the second event, where it seems the problem lies.

    After making the changes suggested by davegugg i have the following for the input form entries (have removed the error messages for empty textboxes and initialize ;
    Please Login or Register  to view this content.
    and this is the revised code for the Receipts & Payments (sheet 9) change event
    Please Login or Register  to view this content.
    Have I adequately explained the problem? I would appreciate any help on this.

  7. #7
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    Do you have the code in a module? That is where it should be.

  8. #8
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    I have now created a separate module which is called from the worksheet change event. This appears to run further but now gets stuck where I am copying data. Annotated below.
    Please Login or Register  to view this content.

    Presumably this is not the correct way to copy the cells of the rows that meet the if criteria. So how do I do this?
    Last edited by upod; 10-22-2009 at 12:56 PM. Reason: Makes easier reading to see all the code instead of one line

  9. #9
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    Right, the problem is you have activecell on both sides of the equal sign. The ActiveCell property only identifies the active cell on the active sheet. So if CoA is not the active sheet, Sheets("CoA").ActiveCell doesn't make any sense.
    Can you use the cell index instead? For example:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    I can't use a fixed reference because the module that precedes is looping down the list checking date criteria. When it finds a row that meet the criteria some of the data is put in the second sheet, and so on.

  11. #11
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    Then you need to assign a variable to stand for that cell you are trying to copy. Its hard to tell where because your code is too spaced out but:
    I think right here you'd go

    Please Login or Register  to view this content.
    You need to dim CellAddress as a String.

    Then when you get to the line that was causing you trouble, you should be able to go


    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    I did as you suggested and I am still getting a run-time error '1004', 'Application-defined or object-defined error' on the line
    Please Login or Register  to view this content.
    Although the code fails at this point the actual date does get pasted to the second sheet: but that is as far as it goes. Any other ideas?

  13. #13
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    I can't see how the date would get pasted but the code would still fail on that line. Did you alter the lines directly after that line? I noticed they also have the activecell property on both sides of the equal sign.

  14. #14
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    Yes, I changed them as follows;
    Please Login or Register  to view this content.
    Thanks for taking the time to look at this by the way.

  15. #15
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    Hmm, I can't think of a reason that it would fail. After you get the error, what does it say CellAddress is equal to when you hold the cursor over it while debugging?

    Otherwise if you could post the workbook, that'd make it easier to troubleshoot.

  16. #16
    Registered User
    Join Date
    06-09-2008
    Posts
    28

    Re: copy cells from sheets based on date, paste to another sheet

    Ok here is a chopped down version of the workbook.

    I have changed the sheet event to 'calculate' and the first part now seems to work when payments are set up but when it comes to the the sheet 9 update, the code it gets stuck.
    Attached Files Attached Files

  17. #17
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    Ok, I see what the problem is. Give me a bit to fix it, I'm pretty busy with my real job today.

  18. #18
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: copy cells from sheets based on date, paste to another sheet

    Ok, I rewrote your code. Here are a couple of issues:
    1) You don't need to select things. Look closely at how I reworked all the "Select" code to make it more efficient. It will run much faster this way.
    2) Your Frequency select between months cells don't work. The Month() function tells you what month the date you put in is in. For example, Month(Date) = 10 (October is the tenth month). Dates are stored as integers in excel. Since you put 1 and 3 in those cells, you are actually looking at 1/1/1900 and 1/3/1900. If you want to add a month, put 30 in those cells and just add the cell value instead of using the month code.
    3) The reason it was giving you this last error was the scope of the variable was local. To make it work, above both modules define it as public. For example: Public yourvariable as String.

    Go through this code and see the changes carefully. I put the revised code near the commented out lines of your code so you could see how they correspond. Then after, go ahead and delete out your code. If it is then not working properly, let me know and we'll tweak some more.

    Please Login or Register  to view this content.

+ 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