+ Reply to Thread
Results 1 to 5 of 5

VBA - If/And/Or/Else - MsgBox after 'Else' stuck in repeat trigger.

  1. #1
    Registered User
    Join Date
    02-08-2012
    Location
    Melbourne, Australia.
    MS-Off Ver
    Excel 2011
    Posts
    2

    VBA - If/And/Or/Else - MsgBox after 'Else' stuck in repeat trigger.

    Hello,

    Very novice VBA user here. Apologies for the clumsy name of this thread, I'm not quite sure how to explain what I'm experiencing.

    I am attempting to implement a solution at work, whereby a summary sheet ('TravelTimeCalculator') is created that can call up all hours that need to be paid for a given staff member in a given month that is not included in a 'job', such as travel time and wait time between jobs. The spreadsheet has multiple sheets that need to be searched to pull this data.

    Essentially, on this summary sheet I have a cell with predefined choices for the staff members name (B2) and another where the month can be selected (B3). The macro then searches all sheets for a row that has that staff members name, the selected month and is designated as 'travel' or 'approved wait time'. Where it finds such a row it is copied onto the summary sheet.

    This much works fine with the following solution;
    Please Login or Register  to view this content.
    However, I would like it to be able to tell the user if no records match the set criteria, so we can differentiate between a true absence of match and a broken macro

    To do this I attempted the following code;

    Please Login or Register  to view this content.
    This appears to work, to a point. If I put in a criteria I know cannot be matched, I get the appropriate pop up. However, on clicking 'okay', the pop triggers again immediately and appears to be stuck in an infinite loop, not allowing the user to do anything else. To get out of this, I have to force close excel.

    I thought it might be that it was stuck looping between the worksheets. However, adding an 'End if' after 'Next WkSht' gives the error 'End if without block if'.

    Does anyone have any ideas of how I could stop the MsgBox pop up loop, or any other way to signify that there are no rows matching the given criteria in the workbook?

    Please do not hesitate to ask for additional explanation if I am not clear. I know am not at all knowledgeable about appropriate terms

    Thank you very much for any assistance you can provide,

    Courtney.
    Last edited by Kalisary; 02-08-2012 at 06:30 AM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA - If/And/Or/Else - MsgBox after 'Else' stuck in repeat trigger.

    Your MsgBox is within the loop going through the first 3,000 rows, so it will pop up for every row that doesn't match your criteria. So, it's not an infinite loop, it's just that you haven't pressed "OK" 3,000 times

    There are more efficient ways of running your code, but you could try something like this:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    02-08-2012
    Location
    Melbourne, Australia.
    MS-Off Ver
    Excel 2011
    Posts
    2

    Re: VBA - If/And/Or/Else - MsgBox after 'Else' stuck in repeat trigger.

    Thank you, Andrew, that works a treat.

    A 3,000 long loop, oh my.

    I would appreciate any input into efficiency, if you have any interest or time to spare, I have noticed it runs a little slow. This solution was very much a mash-up of multiple solutions to similar problems posted on the internet.

    Thank you again,

    Courtney.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: VBA - If/And/Or/Else - MsgBox after 'Else' stuck in repeat trigger.

    Can you post a sample of your workbook?

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: VBA - If/And/Or/Else - MsgBox after 'Else' stuck in repeat trigger.

    Courtney,

    Anything which has to loop through rows is likely to be slow. The usual way around this is to use the .Find method to find values you're looking for, as it saves you having to check every row in turn.

    Something like this, maybe:

    Please Login or Register  to view this content.

+ 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