+ Reply to Thread
Results 1 to 13 of 13

Message Box Efficiency

  1. #1
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Message Box Efficiency

    Hey Everyone,

    I've created a large macro that uses the find function quite often and if the object I'm looking for isn' t found I have a Message Box appear saying " 'Cat' is not found would you like to continue?" . What I have coded now works fine, I'm just curious if there is a way to make this more efficient. Currently, I have about 40 "if" statements of code that look like the below. I want to keep it so that the display message is changed each time... for example if "dog" isn't found I want the display to say specifically "dog" isn't found.

    Any thoughts or ideas?

    Please Login or Register  to view this content.
    Last edited by ashleys.nl; 09-15-2011 at 08:29 AM.

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Message Box Efficiency

    We'd need to see more of the code. I suspect an awful lot of it can be replaced with a loop but can't be sure without seeing it.
    Remember what the dormouse said
    Feed your head

  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: Message Box Efficiency

    Hello ashleys.nl,

    This macro example will search all worksheets in the workbook looking for "Cat". This can be expanded to include other search terms. If the term is not found, you will be prompted if you want to continue. If you have any questions, please ask.
    Please Login or Register  to view this content.
    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
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: Message Box Efficiency

    Quote Originally Posted by romperstomper View Post
    We'd need to see more of the code. I suspect an awful lot of it can be replaced with a loop but can't be sure without seeing it.
    Hey rompertstomper

    Essentially the code has a section of about 40 "if" statements with message boxes that just keep repeating... if A wasn't found? then if B wasn't found and so on... I'm happy with the rest of the code as of now... I was just curious if there was a creative way to turn these "if" statements with message boxes into a loop of a sort while still maintaining a customized message for each object that wasn't found. If B isn't found I want the message box to maintain that "B is not found" and so fourth for "C", "D" etc.

  5. #5
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: Message Box Efficiency

    Hey Leith,

    Thanks for the code, but maybe I described my question poorly... my search functions are all fine. I have no issues with that. Their are numerous find functions throughout the Macro. what I'm trying to improve is the message box statements. Before the macro starts doing actual calculations it goes through about 40 "if" statements with message boxes. If the search object (cat) isn't found it dislpays a message asking if I would like to continue anyway. I'm trying to find a way to make these 40 "if" statements more efficient while maintaining the object description in the display box. For example if "Cat" isn't found I want to specifically say "Cat is not found", and the same for dog, mouse, rabbit etc.

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Message Box Efficiency

    Per my first post, it's too late to optimise at that point - you need to alter the rest of the code so that you haven't got 40 range objects set. Unless you post that code, not much we can do.

  7. #7
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: Message Box Efficiency

    Hey RomperStomper

    Most of the information is sensitive , but this is the just of the code, just imagine the "find" functions and the "msgBox" appearing a lot more often.

    Thanks for your interest and help.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Message Box Efficiency

    Please Login or Register  to view this content.



  9. #9
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: Message Box Efficiency

    Hey SNB

    This is great, but how would I implement it so I'm still assigning my columns and rows? If you look in the code I've quoted previously I had statments such as

    Please Login or Register  to view this content.
    Thank you soo much though!

    Quote Originally Posted by snb View Post
    Please Login or Register  to view this content.

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Message Box Efficiency

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-05-2011
    Location
    St. John's, Canada
    MS-Off Ver
    Excel 2010
    Posts
    70

    Re: Message Box Efficiency

    Hey SNB,

    I'm getting somewhere, I've taken a little twist on what you've given me. I want to keep the ColDog, ColCat, etc. variable names for future reference of people reading the code. So created the below code. But when I try to paste the information from the column I get "Run-Time Error 1004': Application-defined or object defined error"

    Any thoughts?

    Please Login or Register  to view this content.

  12. #12
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Message Box Efficiency

    It's quite hard to suggest the best method without knowing what you are doing with the data (or why you want separate variables) but regarding your last posted code, you need to research what Split does as your usage of it is incorrect.
    Personally, I suspect that you would be better off moving the find operation to a separate routine and then pass the search value to that routine. Something like:
    Please Login or Register  to view this content.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Message Box Efficiency

    Any thoughts?
    Return to the code I suggested.
    First aim at functionality and errorless functioning of the code. After that modifying for future users may be of interest.
    Don't present error messages we can't check: you decided not to post a sample workbook (in spite of various requests to do so), so the consequence is that you habe to do the debugging yourself. There's not such a thing as 'virtual debugging'.

+ 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