+ Reply to Thread
Results 1 to 34 of 34

Trying to continue macro when value is #N/A

  1. #1
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Trying to continue macro when value is #N/A

    Hi,

    I'm trying to solve the following problem. In Excel I need to lookup some value's for which I use the VLOOKUP function. Sometimes I've got a result, but sometimes it's unavailable since it doesn't exist or anything like that, which is fine. However I need to run a loop in which I have to check all the values and when there is a match, it should do something with it. When there is nothing available, it should continue to check. This part I can't get to work.

    I've tried
    Please Login or Register  to view this content.
    and tried

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.
    But they don't work. I would be very happy to find a way to fix this. The full code is as following:

    Please Login or Register  to view this content.
    PS, the 1 to 18 will change to around 1500 in the future, this is just a testcase.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: Trying to continue macro when value is #N/A

    How about just adding
    Please Login or Register  to view this content.
    at start of the code?

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

    Re: Trying to continue macro when value is #N/A

    You don't seem to be using VLOOKUP in that code, if you were you could use IsError to check if there was a problem.
    If posting code please use code tags, see here.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Trying to continue macro when value is #N/A

    Difficult to comment without seeing the request in the context of its workbook but have you considered wrapping the VLOOKUP function in an ISERROR function and testing for the value you have set as the Error part of the ISERROR function.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  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: Trying to continue macro when value is #N/A

    How about just:

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    I've tried like this

    Please Login or Register  to view this content.
    Gives an error at If Range("D46").Value <> "0" Then

    -edit-

    No it works half, it now continues but also copies that sheet which shouldn't. Guess I've to add a i+1 somewhere
    Last edited by larstk; 01-06-2013 at 05:32 PM.

  7. #7
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    I've now tried the GoTo code

    Please Login or Register  to view this content.
    Still get stuck at "If Range("D46").Value <> "0" Then"

    Is the ErrHandler not valid?

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

    Re: Trying to continue macro when value is #N/A

    Why not try IsError?
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Tried the following:

    Please Login or Register  to view this content.
    But it says Compile error, Next without for

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

    Re: Trying to continue macro when value is #N/A

    Quote Originally Posted by larstk View Post
    Is the ErrHandler not valid?
    No - once you've called an error handler, via On Error, error handling is suspended until you either exit the subroutine you're in or use a resume statement. So your error handler will work the first time you call it, but not the 2nd (because, as far as VBA is concerned you're still handling the first error).

    It would work if you changed the line to:

    Please Login or Register  to view this content.
    But using the IsError function is much neater way of dealing with the problem.

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

    Re: Trying to continue macro when value is #N/A

    This is the structure you need:

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Thanks! But now it stops instead of moving on.

    Tried this but wouldn't work either.. :
    Please Login or Register  to view this content.

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

    Re: Trying to continue macro when value is #N/A

    You don't need this.
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    yeah but it stops when an error occurs instead of trying the next one

    Quote Originally Posted by Norie View Post
    You don't need this.
    Please Login or Register  to view this content.

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

    Re: Trying to continue macro when value is #N/A

    Which next one? There's nothing in your code to tell it to look at a cell other than D46. Are you trying to get it to loop through a range of cells?

  16. #16
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Yes but I'm changing one cell (D3) and thereby this changes cell D46 (and some other cells). The whole lot is copied to another sheet and then 1 is added to cell D3 until I've reached all of them.

    This is now the code:
    Please Login or Register  to view this content.

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

    Re: Trying to continue macro when value is #N/A

    What exactly are you trying to do?

    In your first post you mention VLOOKUP but there's nothing in the code for a VLOOKUP.

    Is the VLOOKUP in a cell?

  18. #18
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Yes sorry!,

    Vlookup is in a cell



    Quote Originally Posted by Norie View Post
    What exactly are you trying to do?

    In your first post you mention VLOOKUP but there's nothing in the code for a VLOOKUP.

    Is the VLOOKUP in a cell?

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

    Re: Trying to continue macro when value is #N/A

    What, in words, should the code do?

    Can you attach a sample workbook showing a before/after? That might make things clearer.

  20. #20
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Okay had to change a couple of things to make things private.

    The invoice is the one wich its all about.
    Attached Files Attached Files

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

    Re: Trying to continue macro when value is #N/A

    So you'll have about 1,500 invoices stored in a rough database format, and you want to loop through them, 1 by 1, and put each of them on its own formatted sheet, within the same workbook?

    Is that roughly what you're after?

  22. #22
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Quote Originally Posted by Andrew-R View Post
    So you'll have about 1,500 invoices stored in a rough database format, and you want to loop through them, 1 by 1, and put each of them on its own formatted sheet, within the same workbook?

    Is that roughly what you're after?
    Yes or make a new workbook for each of them. Thing is, most of them will be empty and I don't want them to be copied. Some will "not exist" so those need to be skipped. It's a database with unique members and they all have an unique number.

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

    Re: Trying to continue macro when value is #N/A

    I'm not 100% sure what you are doing but I've got some idea.

    Anywy, try this.
    Please Login or Register  to view this content.

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

    Re: Trying to continue macro when value is #N/A

    Quote Originally Posted by larstk View Post
    Yes or make a new workbook for each of them.
    I've got to ask ... why? You've got them all in a database format, so what's the benefit of saving each of the individually?

  25. #25
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Quote Originally Posted by Norie View Post
    I'm not 100% sure what you are doing but I've got some idea.

    Anywy, try this.
    Please Login or Register  to view this content.
    Great!! Apart from the wrong copying of the "subtotal invoice" it works exactly as I would like to many thanks!

  26. #26
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Quote Originally Posted by Andrew-R View Post
    I've got to ask ... why? You've got them all in a database format, so what's the benefit of saving each of the individually?
    True but I need to sent them out through email. Need to work on that as well. I know there is something like mailmerge but it doesn't work exactly as I want.

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

    Re: Trying to continue macro when value is #N/A

    I noticed that, shouldn't be too hard to fix - perhaps use paste value for the formulas.

    By the way, why don't you just loop through the rows on the 'Wedstrijdstarts compo'?

    Then you wouldn't have any #N/A problems.

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

    Re: Trying to continue macro when value is #N/A

    Pasting them all on to separate sheets doesn't make them any easier to send through e-mail. I'd really have a careful think about this, if I were you

  29. #29
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Quote Originally Posted by Norie View Post
    I noticed that, shouldn't be too hard to fix - perhaps use paste value for the formulas.

    By the way, why don't you just loop through the rows on the 'Wedstrijdstarts compo'?

    Then you wouldn't have any #N/A problems.
    Not sure what you mean.. You mean that you would do a loop in the sheet?



    Quote Originally Posted by Andrew-R View Post
    Pasting them all on to separate sheets doesn't make them any easier to send through e-mail. I'd really have a careful think about this, if I were you
    If you have a suggestion I'm eager to hear. I'm using Gmail and not using any "mail software".

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

    Re: Trying to continue macro when value is #N/A

    I mean instead of looping through a set of arbritary numbers 1,2,3 etc, some of which won't match, actually loop through the numbers on the worksheet plugging them into the invoice sheet.

  31. #31
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    You mean, run the A1 column ("Debiteurnr#") of the "Wedstrijdstarts compo" sheet on the "Invoice" sheet directly? I would like to do that because that would save time and useless processing. But which function do I need for that since VLOOKUP is not going to work for this. And the sheets need to keep their names corresponding to that "Debiteurnr#" which they do now.

  32. #32
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Solved, I think by changing
    Please Login or Register  to view this content.
    Into

    Please Login or Register  to view this content.

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

    Re: Trying to continue macro when value is #N/A

    This is what I was thinking of.
    Please Login or Register  to view this content.

  34. #34
    Registered User
    Join Date
    01-06-2013
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    mac Excel 2011
    Posts
    16

    Re: Trying to continue macro when value is #N/A

    Ah yeah that's a very neat solution! Works even better since it's now adapting to the the range which can change of course. That's very helpful!

+ 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