+ Reply to Thread
Results 1 to 9 of 9

Macro deleting values on wrong sheet

  1. #1
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Macro deleting values on wrong sheet

    The below code is supposed to delete data on the last sheet of the workbook HeadsetOutTimeMay.xls however for some reason it is deleting the data on HeadsetOutTime.xls What am I doing wrong?

    Please Login or Register  to view this content.
    Last edited by randell.graybill; 06-12-2009 at 01:28 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Macro deleting values on wrong sheet

    Hi there,

    Here's a very quick reply as I haven't time to check all of your code at the moment.

    Delete the
    Please Login or Register  to view this content.
    statement - using this is always dangerous unless you really know what you are doing, as your code will continue to run even after errors have been detected. If you delete the statement you should at least get an error message showing which part of your code generated the error, and you can use this as a starting point for further investigation.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Re: Macro deleting values on wrong sheet

    Can anyone help with this?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro deleting values on wrong sheet

    Greg M made a good point - comment out the On Error Resume Next line and step through the routine line by line using F8.... if the May file fails to open the Windows call will fail also at which point the current workbook will be the workbook active at the time the code was invoked (presumably the HeadsetOutTime.xls) ... you could use the workbook object you've created (DstWkb) in your subsequent Sheet calls etc to ensure you're testing against the correct file.

    I would also advise you alter r to be Long rather than Integer ... Integer's boundaries do not encompass all Row values (ie Integer caters up to 32767) ... in VBA Integers are converted to Long "under the hood" so there is no memory gain to be had in using Integer over Long, in fact some have argued it's slower to use Integer (ie Integer -> Long -> Integer)

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

    Re: Macro deleting values on wrong sheet

    As others have said, although 'on error resume next' does have it's place in macros, this is not one of them. I'd definitly get rid of it.
    Also your macro relies heavily on 'ActiveSheet' and 'assumption' of the workbook that the the ranges will currently apply to, both of which are dangerous things in macros.
    Only EVER use 'ActiveSheet' if you REALLY want to reference whatever sheet the user currently has active, and you have no way of knowing that at design time. For all else it is safest to explicitly reference the correct workbook and worksheet.

    See my recommended changes to your code in red below. I have tried to not change your code too much so that you can see how it relates abck to the original code.

    Please Login or Register  to view this content.
    I have not tested the above code, but it should show you at least how you SHOULD be referencing your workbooks and worksheets.
    Last edited by Phil_V; 06-09-2009 at 07:07 AM. Reason: Forgot to change the Integer to Long
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro deleting values on wrong sheet

    Remember to adjust r from Integer to Long...

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

    Re: Macro deleting values on wrong sheet

    Ooops, my bad, my post now edited

  8. #8
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Re: Macro deleting values on wrong sheet

    Quote Originally Posted by Phil_V View Post
    A

    Please Login or Register  to view this content.
    I'm trying to rename the destination sheet being placed in May. I'm changing the sheet abit to be 7 sheets one for each day but I want to get the sheet working for one day before I worry about changing it around to be 7 different sheets (that way not as many names display).

    Thanks for the code I'll try to incorpate it into my code once I get everything else working to optimize my code. I pick up code as I go and only have about two months of VBA experience under my belt now.

  9. #9
    Forum Contributor
    Join Date
    04-03-2009
    Location
    USA, California
    MS-Off Ver
    Excel 2007
    Posts
    385

    Re: Macro deleting values on wrong sheet

    Thank you all for the help.

+ 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