+ Reply to Thread
Results 1 to 10 of 10

How to improve this macro

  1. #1
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    How to improve this macro

    I have a membership database spreadsheet with about 1000 members, and have developed various macros to extract data for various purposes (different mailing lists, email address files etc). A subroutine in all these checks the databases for empty cells in columns relevant to the macro, and reports an error message if it finds an empty cell (with the location of that cell). The macro basically just steps through every cell in the programmed columns and checks if empty.

    Currently this subroutine is tailored for each macro, so that it only checks the relevant columns, as sometimes an empty cell can be valid.

    I'd welcome suggestions on two points: is there a more efficient way of testing for empty cells, while at the same time being able to report the location if found. Secondly I'd like to modify the macro so that I could define a list of the columns to check at the beginning of the macro (maybe a comma separated string variable?), so that I can reuse the main code in each macro, rather than have umpteen versions of it.

    I've included the current code here, but also attached the file, together with a sample membership database (dummy data) at the end.

    Thanks for any ideas.

    Please Login or Register  to view this content.
    Check Membership.xlsMembership (Test Data B).xls

  2. #2
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How to improve this macro

    What is the overall procedure? Do you want it to look through all the workbooks and only stop if a blank cell is found, or process each workbook one at a time when so directed? If all at once, do you want it to just stop with that workbook left open so the value can be corrected? Also, are all the workbooks in the same folder? My idea is to have a Parameters sheet in your main workbook with a list of all the workbook names, maybe preceeded by a column with the folder names (if needed), and followed by columns containing the columns to be searched. The code could then just go down the list and process each, and if changes are made in the future, just update the list without any coding changes needed. The code would be pretty simple.
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  3. #3
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: How to improve this macro

    To answer you questions:

    1. There is only one workbook, with one sheet) containing the membership details, and the macro above has already checked that it exists and has opened it. There are multiple workbooks containing only a "setup" macro, and each of these does different "functions" in terms of extracting and processing the membership data into a "data extract" workbook for the various uses (mailing lists, email address files etc). That "setup" workbook currently only has some instructions and a run macro button in sheet 1, and there are no other sheets.

    2. The files (membership.xls, setup.xls and data extract.xls) for each "function" are all in the same "working folder" for that "function" (e.g. there are separate folders for "make mailing list for newsletters", make membership cards", "make group xx email list" etc.) A copy of the master membership.xls is manually copied into the "working folder" before the macro is run.

    3. If a blank cell is found the setup macro displays an error message showing the location of the blank cell, then closes the membership.xls workbook before terminating. The user will then have to manually open the membership.xls workbook, correct the problem, save it, then start over. I do it that way because if there is a blank cell the user will probably have to contact the membership secretary to obtain the missing data (or probably a new version of the membership.xls workbook), which may take a day or so.

    4. The macro in my original post is just one subroutine part of an overall "function" specific specific macro. My thought was to just have the configuration information at the start of this function specific macro, which is then used by a reusable "check membership" sub routine, rather than have a different version of this subroutine for each overall "function". This way the parameters are in the main macro itself, rather than tucked away in a, probably hidden, worksheet. I think that this would make it easier to maintain if and when someone else has to take this lot over.

    I hope this is a bit clearer, if not please ask.

  4. #4
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: How to improve this macro

    ooops, posted reply twice, sorry!

  5. #5
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How to improve this macro

    I'm afraid I'm lost between all the workbooks, macros, and functions, and what is running from where and when. If you could narrow it down to focus on the specific issues -- a more efficient way to find blank cells (yes, there is definitely room for improvement), and defining the list of columns to be checked in each workbook, that would help. Do you want to pursue the Parameters sheet idea I referred to? If so, would that sheet be in the same workbook that the code is being run from? Should the code loops through the list and open each workbook, or will it already be open, or will it just process one workbook at a time? Sorry for being dense.

  6. #6
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: How to improve this macro

    Sorry, I'm not explaining myself very well.

    While there are many workbooks and macros that I'm working on and using, as far as this question is concerned there are only two workbooks involved (the ones attached to my original post):

    Check Membership.xls - this workbook contains only one sheet (with a macro start button and instructions), and the setup macro (the code I listed in my original post).
    Membership (Test Data B).xls - this is a test version of the membership database, with only 100 members, and anonymous data. This is the data that the setup macro is checking for spaces.

    The objective is to enable the choice of the columns to be searched for blanks to be parameter driven, those parameters could be in a sheet (hidden) in the code workbook, or set up as a variable(s) at the beginning of the code itself - I'd prefer the latter, it's less complex for ongoing support purposes.

    I don't think that you need to know this but:
    Eventually, this "blanks check" macro will be used as a subroutine in what I've referred to as the "function macros" in my previous post, which need to do the "blanks check" before they start producing all the other different data files. To try and simplify my question I had just extracted the blanks check bit of code into it's own macro.

  7. #7
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How to improve this macro

    Give this a try. I cleaned up several other parts of the code that I thought were unnecessary also.

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: How to improve this macro

    Thanks Nate,

    I'll set this up and run some tests to see how it works and if it does what I'm looking for. The code that searches for blanks certainly looks a lot slicker than what I had. I'll post back with comments.

  9. #9
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: How to improve this macro

    OK, I've made a few tweaks to Nates suggested code and have now got it doing what I want. The changes I've made are:

    1. Changing the variables from Long to Integer (32,767 is more than adequate).
    2. Moving the assignment of the ColList() array to the main "control" sub, so that I can choose the columns I want checked differently for each project by setting the ColList value to 0.
    3. The Membership Database has two summary rows at the bottom, so I delete these before the finding the end of the data area to check for blank cells.
    4. If a blank cell is found, display an error message containing the cell reference, and close the Membership Database without saving it (the user will normally have to obtain a corrected copy of the database from our membership secretary, not do any changes themselves).

    I think that the way I have it now I can use the Sub CheckMemsData and Sub ErrMessage routines in a "common code" module in other projects, each with a just a project specific Main sub. I'll add more reusable routines to the the "common code" module as I develop more projects, and can just copy that module in each time (just have to remember to add the declarations for any new variables as I do this).

    QUESTION - do the variable declarations all have to be in the module where the first run sub is (ie Main) or can I put the ones relevant to the common code routines at the top of the "common code" module? Will the compiler find them wherever they are?

    If anyone's interested I've included the revised code below.

    Thanks again to Nate for his help.


    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: How to improve this macro

    1. Changing the variables from Long to Integer (32,767 is more than adequate).
    It is my understanding that, the way Office products store variables, it's actually more efficient to use Long than Integer or Byte, so I just use Long for everything, even though it goes against my frugal nature

    QUESTION - do the variable declarations all have to be in the module where the first run sub is (ie Main) or can I put the ones relevant to the common code routines at the top of the "common code" module? Will the compiler find them wherever they are?
    As long as the variables aren't defined within a subroutine, code in other modules can use them.

    I think that the way I have it now I can use the Sub CheckMemsData and Sub ErrMessage routines in a "common code" module in other projects, each with a just a project specific Main sub. I'll add more reusable routines to the the "common code" module as I develop more projects, and can just copy that module in each time (just have to remember to add the declarations for any new variables as I do this).
    You have the right idea. The following lines:

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    could also be moved to the common code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Improve macro
    By ngkj in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-30-2012, 02:25 AM
  2. [SOLVED] Improve macro help
    By fullers in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-21-2006, 06:00 AM
  3. Help to improve macro
    By unni5959 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-19-2005, 07:05 PM
  4. Can you help me to improve this macro?
    By Dario de Judicibus in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-15-2005, 07:05 AM

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