+ Reply to Thread
Results 1 to 8 of 8

Why do Macros work sometimes?

  1. #1
    Registered User
    Join Date
    11-25-2007
    Posts
    46

    Why do Macros work sometimes?

    The last few weeks I have been visiting this board for help with a macro (thanks). But the macro seems to work one week and not the next. Probably most is due to my poor programming, but it still amazes me that each week I have to go in and "fix" a macro that worked last week.

    Two weeks ago the macro worked. Then it stopped working the next time I ran. The code was all in the "This Workbook" sheet and the forum suggested I move the code to a module. I did and it worked again.

    This week the same macro gags on:

    Please Login or Register  to view this content.
    yet this piece of code has worked for many weeks. Suddenly now it does not work. Below is my change that is working "so far". I understand now it is probably better to be quite specific with what cells to select. Yet the code worked for a long time.

    Please Login or Register  to view this content.
    HTML Code: 
    for the details of my current issue.

    I am a novice coder and I have borrowed and cobbled together the code I have. The purpose of my macros is to automate a tedious and long repeated analsyis of some data that updates each week. I would love to have the macro actually work without further tweeking.

    Are there any other dumb, beginner mistakes in the cross posted code? General rules of thumb like "always put ActivesSheet before commands like Cells"?

    Thanks

    Cam
    Last edited by ctmurray; 01-11-2008 at 10:58 PM. Reason: Fix title

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Why would we want to go to another forum to find the details of your question?

  3. #3
    Registered User
    Join Date
    11-25-2007
    Posts
    46
    Actually the cross post is to this forum - I probably used the wrong term in my description. I just did not want to re-post the same details. Sorry for the confusion.

    Cam

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885
    Hi ctmurray,

    Try using this code. It's a little bit more streamlined and shouldn't run afowl of sheet references or result in range/cell errors.
    Please Login or Register  to view this content.
    I commented out the variables 'start', 'pvalue', 'rvalue' and 'svalue' since they weren't used anywhere in the code. If they're used elsewhere in the module, uncomment them and declare their data type (Integer, String, etc.)

  5. #5
    Registered User
    Join Date
    11-25-2007
    Posts
    46

    Thanks

    Thanks for the code, I will use and read over to learn some better coding skills (which I need). Some of the dimensioned items were extra, probably left over from code I cut. I think I got most this code from somewhere else. As I was troubleshooting this code I noticed that none of the dimensioned items were specified as intergers (or anything else). So I did make the ones in use into intergers.

    Thanks again.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Cam

    I'm pretty sure the problem has absolutely nothing to do with how you've dimensioned your variables.

    The issue is more likely to be with unqualified references and/or the use of select.

    Firstly if you have something like this:
    Please Login or Register  to view this content.
    Then it will refer to cells on what VBA considers the active sheet, which may or may not actually be the one you want to refer to.

    Secondly there is normally no need to use Select and in fact it can cause problems.

    There's also no need to use Activate as long as you properly reference everything and you should use Long not Integer.

  7. #7
    Registered User
    Join Date
    11-25-2007
    Posts
    46

    Can you elaborate?

    I appreciate the comment.

    I am adopting the code provided pjoaquin which does not use the Cell.Select function. But why does select not work in general?

    Why use long vs integer?

    Cam

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Cam

    I've not had time to look at your original code.

    But to answer your questions.

    1 You don't need to use select.

    2 VBA will automatically convert Integer to Long, so if you use Integer it may have some impact on performance.

+ 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