+ Reply to Thread
Results 1 to 12 of 12

Error 2023: How to identify cell in For Each Loop?

  1. #1
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    682

    Error 2023: How to identify cell in For Each Loop?

    Please Login or Register  to view this content.

    The program works fine on most files, so there is no code issue.

    How to add some code to display which cell causes the program. Such as sheet name, row number, column number. This part of code will not be part of program, but it is used for programmer to find out which cell causes the issue. Probably some cells come with formulas and it does not work well with Trim function. Now I would like to add some code to find out which cell causes the first Error 2023.

    Maybe I should use If Error Then Display message for user.

    Thanks.

  2. #2
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    682

    Re: Error 2023: How to identify cell in For Each Loop?

    Maybe I should simplify the question, each time, when code runs into For loop, For Each OneCell, now how can I know which cell does the OneCell refers to at time time point?

    Thanks.

    Modified code should be like this:


    Please Login or Register  to view this content.
    Last edited by VAer; 11-11-2018 at 10:07 PM.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error 2023: How to identify cell in For Each Loop?

    Please Login or Register  to view this content.
    Add the Debug.Print line. Run your code. When it errors, look at the Immediate Window (Ctrl+G) in the VBA Editor, and the last cell address is the one that it errored on.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    682

    Re: Error 2023: How to identify cell in For Each Loop?

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    Add the Debug.Print line. Run your code. When it errors, look at the Immediate Window (Ctrl+G) in the VBA Editor, and the last cell address is the one that it errored on.
    Could you please look at my second post and how to modify my code? That is a better code structure than what I have right now. I would like users to know which cell causes the program without looking at Immediate Window. Thanks.

    Edit: moreover, Debug.Print does not show which sheet, it does not really tell enough information. See attached.
    Attached Images Attached Images
    Last edited by VAer; 11-11-2018 at 10:21 PM.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error 2023: How to identify cell in For Each Loop?

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    682

    Re: Error 2023: How to identify cell in For Each Loop?

    By the way, should I put On Error right before OneCell = WorksheetFunction.Trim(OneCell)? INSIDE IF statement? I guess it does not make sense to put it Outside For loop, since there are other codes after this For loop.

    What does vbExclamation, "Trim Error" mean?

    Does OneCell.Address cell its sheetname? It seems the answer is NO to me.

    Thanks.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error 2023: How to identify cell in For Each Loop?

    Put On Error before the loop. No need to repeatedly turn it on within the loop. It's like a light switch, once it's on, it's on. You can turn it off after the loop ends with On Error GoTo 0

    Select the MsgBox keyword in the code and press F1 on your keyboard for help on that function.

    OneCell.Address(External:=True) will give the sheet name.

  8. #8
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    682

    Re: Error 2023: How to identify cell in For Each Loop?

    Quote Originally Posted by AlphaFrog View Post
    Put On Error before the loop. No need to repeatedly turn it on within the loop. It's like a light switch, once it's on, it's on. You can turn it off after the loop ends with On Error GoTo 0

    Select the MsgBox keyword in the code and press F1 on your keyboard for help on that function.

    OneCell.Address(External:=True) will give the sheet name.
    I like the idea of On Error GoTo 0, BUT what if there are other codes ? Does it bother other error handler? Take below code for example.

    Regarding sheet name, I used Ws.Name and it works.

    By the way, I think you should not put Exit Sub before TrimError:


    Please Login or Register  to view this content.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error 2023: How to identify cell in For Each Loop?

    Quote Originally Posted by VAer View Post
    I like the idea of On Error GoTo 0, BUT what if there are other codes ? Does it bother other error handler? Take below code for example.
    Looks good.


    By the way, I think you should not put Exit Sub before TrimError:
    If you don't, you'll get the msgboxes displayed even if you don't have an error. The error handlers will run if you don't exit. It wasn't an accident that I put it in there.

  10. #10
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    682

    Re: Error 2023: How to identify cell in For Each Loop?

    Quote Originally Posted by AlphaFrog View Post
    If you don't, you'll get the msgboxes displayed even if you don't have an error.
    How come? I have Exit Sub right before TrimError :

    I have Exit Sub right before each error handler.

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error 2023: How to identify cell in For Each Loop?

    By the way, I think you should not put Exit Sub before TrimError:
    Then why did you say this? That's what I commented on.

  12. #12
    Forum Contributor VAer's Avatar
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    682

    Re: Error 2023: How to identify cell in For Each Loop?

    Quote Originally Posted by AlphaFrog View Post
    Then why did you say this? That's what I commented on.
    Actually, I should say: You should not put Exit Sub right after Next OneCell, since there are some other codes. It is just miscommunication.

+ 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. Trying to identify data causing Infinite Loop
    By tarun174 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-16-2014, 10:01 AM
  2. Replies: 1
    Last Post: 02-08-2013, 12:24 PM
  3. Loop through worksheets and identify name of table on each
    By holmes123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2012, 01:16 PM
  4. [SOLVED] Error 2023 - triggered by variable - Cant find mistake
    By Taktiker in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2012, 09:11 AM
  5. [SOLVED] Loop trough table cells, identify alignment and indent according to conditions (VBA)
    By christensen in forum Word Formatting & General
    Replies: 6
    Last Post: 08-15-2012, 08:55 AM
  6. Using a loop to identify a string value
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2012, 06:55 PM
  7. Identify Change in Grade or Position - Loop
    By ronanm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-14-2011, 09:23 AM

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