1

using On Error Resume Next to skip any error on the below code,But it dose not work.
I got error File not found on the second VBA name command.
I know that code can be modified to check existence of the file before rename.
I need to know why On Error Resume Next did not skip that error.
Thanks for your helping.

Sub Name_Test()

 On Error GoTo Pause
 
    Dim oldName As String: oldName = "C:\Users\Waleed\Desktop\Test.txt"
     Dim newName As String: newName = "C:\Users\Waleed\Desktop\Done.txt"
      Name oldName As newName 
Pause:

  On Error Resume Next
  
    Application.Wait Now + TimeValue("00:00:01")
     Name oldName As newName
      ThisWorkbook.Save
    
End Sub     
Waleed
  • 847
  • 1
  • 4
  • 18

3 Answers3

3

Please, try this updated code. It uses On Error GoTo -1 to clear the previous error from memory:

Sub Name_Test()
 On Error GoTo Pause
     Dim oldName As String: oldName = "C:\Users\Waleed\Desktop\Test.txt"
     Dim newName As String: newName = "C:\Users\Waleed\Desktop\Done.txt"
      Name oldName As newName
Pause:

    Application.Wait Now + TimeValue("00:00:01")

     On Error GoTo -1 'it clears the previous error from memory!
     
     On Error Resume Next
     Name oldName As newName
     If Err.Number <> 0 Then Stop 'it has been caught, but no error has been raised, anymore...
      'ThisWorkbook.Save
End Sub

"Note: Err.Clear is different from On Error Goto -1. Err.Clear only clears the error description and the error number. In the case of On Error GoTo label, it doesn’t completely reset it (from memory). This means that if there is another instance of error in the same code, you will not be able to handle it before resetting, which can be done with On Error Goto -1 and not using Err.Clear"

I found the above note (or something with a similar meaning) some years before, when searched to clarify this aspect...

FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • @user22 Glad I could help! I also placed a note after the adapted code. I remembered I have it somewhere in my testing environment and found it... Like I wrote there, I am not Its father. I found it somewhere on the internet (I do not remember where). Not exactly with the same words. I wrote it (in my workbook) after some time, from what I could remember, considering being important... – FaneDuru Feb 01 '22 at 10:42
  • 1
    Possibly helpful [Difference between On Error goto 0 and On Error goto -1](https://stackoverflow.com/questions/14158901/difference-between-on-error-goto-0-and-on-error-goto-1-vba/14159999#14159999) – T.M. Feb 01 '22 at 14:26
  • This bad code. If the first `Name succeeds, the code will continue and execute the second `Name` – chris neilsen Feb 14 '22 at 23:28
  • @user22 I do not have a problem with that. I only answered the question as it was formulated: **Why `On Error Resume Next` did not skip error**. I would never use such a code. I only tried explaining how such an error should be handled. If you would explain what you try accomplishing I think I could suggest another way to handle it. If you need waiting for a process to save the workbook, or something similar, I could advice on the issue, too. But, it is only your choice to set the accepted answer! Anyhow, I do not care too much about notoriety. It is only a matter of compliance with the rules – FaneDuru Feb 16 '22 at 08:17
  • @chris neilsen I would never use such a code! I only tried answering the question in the way it has been formulated: **Why On Error Resume Next did not skip error**... Let us say, a didactic approach... :) – FaneDuru Feb 16 '22 at 08:19
2

You'll only want to execute Name again if the first one fails. More conventional error handling would be like this

Sub Name_Test()
    On Error GoTo EH
 
    Dim ErrCnt As Long
    Dim oldName As String: oldName = "C:\Users\Waleed\Desktop\Test.txt"
    Dim newName As String: newName = "C:\Users\Waleed\Desktop\Done.txt"
    Name oldName As newName
Exit Sub

TryAgain:
    Application.Wait Now + TimeValue("00:00:01")
    Name oldName As newName
    ThisWorkbook.Save
Exit Sub

EH:
    ErrCnt = ErrCnt + 1
    If ErrCnt = 1 Then
        Resume TryAgain
    EndIf
End Sub
chris neilsen
  • 52,446
  • 10
  • 84
  • 123
  • It works perfectly, But please: What `advantages` of your answer comparing the answer by `“FaneDure”` (apart from adding `Exit Sub`) ? – Waleed Feb 15 '22 at 14:17
  • @user22 On Error GoTo -1 is undocumented by Microsoft and I've seen warnings by reputable sources against using it (cpearson.com I think, but I can't find it atm). As I've shown you don't you don't have to use it, so why take the risk. All the other code shown here has the bug of always executing the retry. And IMHO the logic flow through my code is much clearer – chris neilsen Feb 15 '22 at 18:44
0

On Error GoTo -1 is effective in turning off an error handler but does not appear in Microsoft documentation On Error statement.

If you find it to be a problem in future development you should be able to avoid it.

Sub Name_Test()

    On Error GoTo Pause
    Debug.Print "On Error GoTo Pause"
    
    Dim oldName As String: oldName = "C:\Users\Waleed\Desktop\Test.txt"
    Dim newName As String: newName = "C:\Users\Waleed\Desktop\Done.txt"
    
    Name oldName As newName
    
    ' To test, comment line above
    Debug.Print " No error."
    
Pause:
    
    If Err <> 0 Then
        Debug.Print Err.Number, Err.Description
        Resume resetErrorhandlerPause
    Else
        On Error GoTo 0
    End If
    
resetErrorhandlerPause:

    On Error Resume Next
    Debug.Print "On Error Resume Next"
    
    Application.Wait Now + TimeValue("00:00:01")
    Name oldName As newName
    If Err <> 0 Then
        Debug.Print Err.Number, Err.Description
        Debug.Print " Error bypassed with On Error Resume Next"
    End If
    
    ' ThisWorkbook.Save
    
End Sub
niton
  • 8,771
  • 21
  • 32
  • 52