+ Reply to Thread
Results 1 to 6 of 6

Msgbox when nothing found through range loop

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Msgbox when nothing found through range loop

    I'm having a bit of a struggle trying to construct this code.

    I realised I don't have any error trapping in the code and am looking to add a message box if the loop doesn't find the value entered into the userform label. The code is basically something along the following lines and am not sure how to put the message box in the code (maybe I'm just being bit thick but I can't get my head around the lop until end but no values found, some help would be great).

    [CODE]
    For each C in range

    with sheet1
    paste some values somewhere
    end with

    find bottom row in range

    with sheet 2
    paste some values somewhere on bottom row
    end with

    next c

    userform1.hide

    msgbox "all done"
    [\CODE]
    Last edited by Sc0tt1e; 06-29-2016 at 09:07 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,466

    Re: Msgbox when nothing found through range loop

    Three choices spring to mind. If it is a "key" field and matches data in a column, you could use Countif to check if the value is there (before you start the loop). Or, you could use a counter if the number of matches is of interest; display your message if it is zero. Or you could use a Boolean "flag" to indicate that a match has been found. Again, display your message if the flag is False.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Msgbox when nothing found through range loop

    Maybe:

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Re: Msgbox when nothing found through range loop

    Genius idea on the count, I keep forgetting to use worsheet functions to solve problems within VBA. So I have added the following

    [CODE]
    With application.worksheetfucntion
    count = .countif(range,value)
    end with

    If (count=0) then
    msgbox "It isn't bloody here OK!!!!"

    else

    Previous code

    [CODE]

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,466

    Re: Msgbox when nothing found through range loop

    @John: the Else y = y is redundant. You just won't be incrementing y.

    @Sc0tt1e: You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Msgbox when nothing found through range loop

    @ TMS. Duh Me.

+ 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. [SOLVED] Skip MsgBox if found error
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-10-2015, 08:59 PM
  2. [SOLVED] Need VBA to find letter A in Column A:A, select range, popup MsgBox then loop
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-28-2015, 04:05 AM
  3. Loop, if found, increase range variable.
    By NewGuy OnBlock in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2015, 05:46 PM
  4. Replies: 4
    Last Post: 04-03-2014, 12:19 AM
  5. [SOLVED] nested loop for copying range: error no cells were found
    By Edejager in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-09-2013, 08:52 AM
  6. How to exit a loop once the value is found in the range
    By Suety in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-28-2010, 01:30 PM
  7. Msgbox until a specific string is found.
    By excelkeechak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-22-2009, 09:43 PM

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