+ Reply to Thread
Results 1 to 10 of 10

"On Error" activates here and I don't know why

  1. #1
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    "On Error" activates here and I don't know why

    I've written the below code which goes through a worksheet (after I've imported some text files), and deletes the header rows. The code works except for one thing. If there are no duplicates, it activates the "On Error" code. Can anyone tell me why this might happen? If there are duplicates, it removes them and everything works fine.

    I was thinking I could put a counter in there and if there are no duplicates found, then I could "exit Sub" but I feel like I'm missing out on understanding something here.

    Thanks if you can help

    Please Login or Register  to view this content.

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

    Re: "On Error" activates here and I don't know why

    If the code doesn't error then after End With execution moves straight to the message box.

    Putting an Exit Sub after the End With should stop the message box being shown if there are no errors.

    What error are you trying to catch anyway?
    If posting code please use code tags, see here.

  3. #3
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: "On Error" activates here and I don't know why

    Hi, njs27,

    if dealing the deletion you better start at the bottom and work up in order to avoid manipulation of the counter.

    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  4. #4
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    Re: "On Error" activates here and I don't know why

    Quote Originally Posted by Norie View Post
    If the code doesn't error then after End With execution moves straight to the message box.
    yes. I don't understand why. Doesn't it have to be caused by something tripping the "On Error" part of the code at the top?


    Quote Originally Posted by Norie View Post
    Putting an Exit Sub after the End With should stop the message box being shown if there are no errors.
    Yes, that would be simpler. But I still don't understand why I would need to?

    Quote Originally Posted by Norie View Post
    What error are you trying to catch anyway?
    Any errors that might happen.

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

    Re: "On Error" activates here and I don't know why

    Code execution moves from top to bottom, so it's eventually going to reach the message box as nothing's stopping it.

    What errors do you expect?

    As far as I can see there's nothing in the code that's going to cause an error.

  6. #6
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    Re: "On Error" activates here and I don't know why

    Quote Originally Posted by Norie View Post
    Code execution moves from top to bottom, so it's eventually going to reach the message box as nothing's stopping it.
    Interesting. Thank you. Where should I be putting that ErrorCatch code then?

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

    Re: "On Error" activates here and I don't know why

    Do you need it?

  8. #8
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    Re: "On Error" activates here and I don't know why

    Quote Originally Posted by HaHoBe View Post
    Hi, njs27,

    if dealing the deletion you better start at the bottom and work up in order to avoid manipulation of the counter.

    Please Login or Register  to view this content.
    Ciao,
    Holger
    Thanks, Holger. That's actually why I put in this part:
    Please Login or Register  to view this content.
    but I like your way better! Thanks!

  9. #9
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    Re: "On Error" activates here and I don't know why

    Quote Originally Posted by Norie View Post
    Do you need it?
    I may not need it here, but I will probably need it in some code eventually, so I'd just like to know where it's supposed to go. Is it dependent on the type of error?

  10. #10
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2016, 32-bit
    Posts
    92

    Re: "On Error" activates here and I don't know why

    D'oh! Ok I missed a fundamental piece of Error Handling code - that I NEED the Exit Sub before the end block. Sorry!

+ 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. Excel 2010 (Run-time error '13' type mismatch) "Debug" and "Continue" Grayed out.
    By Jeronimo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2012, 06:42 PM
  2. [SOLVED] How to Count number of "Error" and "OK" after the word "Instrument" found in table row
    By eltonlaw in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-17-2012, 06:26 AM
  3. Problem using "Cells" in "Range" "400" error
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2009, 05:46 PM
  4. What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 PM
  5. IF(COUNTIF(Thursday,"John Doe")>1,"error"," ")
    By jayd77 in forum Excel General
    Replies: 3
    Last Post: 10-26-2005, 09:17 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