+ Reply to Thread
Results 1 to 24 of 24

Object Variable Not Set

  1. #1
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Object Variable Not Set

    I’m sure this is a relatively simple fix, but I’m unable to find the answer.
    This code is part of a routine that copies and pastes from “CASH RECEIPTS” sheet to “YEARLY TOTALS” sheet in the same workbook.
    rngCash is assigned as a Range variable .
    In the various fixes I’ve tried, the code worked to varying degrees. Now I’m confused.
    Any help would be appreciated.
    Please Login or Register  to view this content.
    There is a workbook attached for reference
    The Macro is “LoopHell”

    Thanks
    Attached Files Attached Files
    Last edited by max57; 01-03-2010 at 01:43 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,480

    Re: Object Variable Not Set - Problem

    I never saw a Dim for the Worksheet, this may work though
    Please Login or Register  to view this content.

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello ,

    Here are two ways to fix the problem...
    Please Login or Register  to view this content.
    In VBA it is rare to have to select an object perform performing an action on it. You will see I removed the rngCash.Selectstatements.
    Last edited by Leith Ross; 01-03-2010 at 01:10 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    Thanks Dave!

    That fixed it.

    Much obliged.

  5. #5
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,480

    Re: Object Variable Not Set - Problem

    Similar problem in the other macro
    change to
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    Hello Leith,

    I did omit the 'rngCash.select' upon your suggestion and it worked with Dave's suggestion.

    Isn't the item method in this case referring to Rows?
    In my case, I'm going across a row, hence the need for the offset-which I neglected in my code but have since added in.

    Now my problem is just getting the Date cell to increment in the paste procedure.

    I have to take a look at why this is not happening all of a sudden.

    Thanks much Leith!

    Mark

  7. #7
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    Hi Dave,

    I thought that if I declared it as a public variable, that it wouldn't have to be dimmed in each module.

    I've since removed it from there and dimmed it in each module.

    At this point, I'm just beginning to understand enough VBA to confuse myself.

    Mark

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello Mark,

    The Item property is a index into the array. The array is read from top left cell down to the bottom right cell. This why it works for either a single dimension array of cells for a row or column.

  9. #9
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    Hi Leith,

    Following that train of thought, is it possible to loop through the entire CASH RECEIPTS page, copying all of the values into an array and then paste the entire array to the YEARLY TOTALS sheet? This would avoid having to go back and forth between the two pages.

    I'm beginning to look at arrays as a method to handle such things but my knowledge is minimal at best.

    Thanks
    Mark
    Last edited by max57; 01-03-2010 at 02:22 PM.

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello Mark,

    If you only need values to be copied then the quickest way is to use an array. The system performs a "block transfer" with arrays. That is. all the data is written at once and not a single element at a time. You can copy the range into a Variant variable without having to dimension the rows and columns to be used. This method only works with the Value of a range.
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    Hi Leith,

    With what you're suggesting,wouldn't
    the ranges being copied have to have identical layouts in order to facilitate this type of method? This method is employed with the other two sheets in this workbook.

    Regarding the CASH RECEIPTS sheet, which does not follow the same layout, is it possible to loop through the rngCash.values, store them in a temporay(?) array, then when the loop is finished, dump them into the YEARLY TOTALS sheet,much the same as dumping a block range?

    Regards
    Mark

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello Mark,

    It sounds like the "CASH RECEIPTS" is a disparate (non contiguous) range of cells. You can loop through this range and then create a new range object of contiguous cells. It would help to see a sample of the data layout or your workbook.

  13. #13
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    Leith,

    Sorry about that. I had uploaded earlier in the thread.
    Here is the working version.
    There are two procedures - one for CASH RECEIPTS, the other for the two AMEX sheets.

    My goal is to incorporate both procedures to run with one call.

    Thanks for your time!

    Mark
    Attached Files Attached Files

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello Mark,

    Thanks for the workbook. I need to restart my computer because there seems to be a problem with file converter. I have Excel 2003 and downloaded the file converter for Office 2007. But as usual, there seems to be a glitch or two.

  15. #15
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    HI Leith,

    I saved a copy in 2003.
    See if this is easier.

    Mark
    Attached Files Attached Files

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello Mark,

    Thanks. I just got my system restarted and was able to convert the original file.

  17. #17
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello Mark,

    After looking at the file, I am not sure were you want the "Cash Receipts" to be copied to. Is it the "Yearly Totals" sheet?

  18. #18
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set - Problem

    Hello Mark,

    Disregard that last post. I found what I was looking for after I ran the macro.

  19. #19
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    Hi Leith,

    The Macro LoopHell will copy the data to the Yearly Totals sheet.

    The Macro just below that will copy the two Amex sheets.

    Mark

  20. #20
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set - Problem

    I did write a simple Sub calling the two procedures.
    while renaming the procedures in the process.
    Please Login or Register  to view this content.
    It seems to be working fine.

    Mark

  21. #21
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set

    Hello Mark,

    Here is the revised macro and it has been added to the attached workbook.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  22. #22
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Thumbs up Re: Object Variable Not Set

    Hi Leith,

    Thanks for your time.
    Very interesting way of coding it.
    I have to study this and work with it in order get to know it.

    I ran the macro in the attached workbook and it worked fine. On a second run, it only pasted the dates.

    I copy and pasted it into another copy, but in Excel 2007. It ran exactly the same.

    I did finish my workbook and have attached for you perusal.

    Leith, thanks for all your ideas and suggestions.
    Quite a learning experience.

    Ciao!
    Mark
    Attached Files Attached Files
    Last edited by max57; 01-03-2010 at 08:40 PM.

  23. #23
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Object Variable Not Set

    Hello Mark,

    I found the problem. The first time I defined the destination range to be "B3:H3", but the next time around it returns only a single cell "B19" as the destination range. I changed one line and added another line to the macro to fix it. The changes are in blue.
    Please Login or Register  to view this content.

  24. #24
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Object Variable Not Set

    Hello Leith,

    Very,very nice!
    Very fast, also.
    I really have to dissect this code for ideas and techniques.

    My humble thanks for your time and efforts.

    Regards
    Mark

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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