+ Reply to Thread
Results 1 to 4 of 4

Code that sometimes works and sometimes doesn't

  1. #1
    Registered User
    Join Date
    11-19-2012
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    8

    Code that sometimes works and sometimes doesn't

    Hi Guys

    I wonder if someone could help me. Code below is only part of my macro. The problem is that it sometimes works and it sometimes doesn't. Sometimes it will freeze and it will not save the changes, sometimes it will not print. After i press X to close excel sometimes it takes 3-5min to close. I would be grateful for tips how to improve this code to run smooth and without errors.




    Please Login or Register  to view this content.
    Last edited by kat14; 02-06-2013 at 04:17 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Code that sometimes works and sometimes doesn't

    Hi,

    this might only be part of why it doesn't always work, but when you are assigning values to variables here
    Please Login or Register  to view this content.
    you don't reference the full object model. This means that excel will assign a range to the variable from the active sheet. And I assume that can vary from time to time. Try using it like this
    Please Login or Register  to view this content.
    like you did with your other variables.


    edit: also, one more thing. If you declare variables like this
    Please Login or Register  to view this content.
    then only r6 will be a range, r...r5 will be variant. You can use
    Please Login or Register  to view this content.
    Last edited by RHCPgergo; 02-06-2013 at 05:02 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Code that sometimes works and sometimes doesn't

    I've got another thing to add. It is redundant to reference a range (and its property) like this:
    Please Login or Register  to view this content.
    Instead of Range(cell3.Address), you can just use cell3, because it is already a range. Also, ThisWorkbook is given, since it's an event macro so it's obvious that you're referencing the workbook you store the code in.
    Please Login or Register  to view this content.
    This line does the same.
    Last edited by RHCPgergo; 02-06-2013 at 05:10 PM. Reason: grammar

  4. #4
    Registered User
    Join Date
    11-19-2012
    Location
    london
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Code that sometimes works and sometimes doesn't

    Thank You for Your replay. I will make those changes. Please let me know if someone has any more code improvement ideas.

+ 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