簡體   English   中英

我沒有使用 on error resume next 獲得所需的值

[英]I am not getting the desired value using on error resume next

我正在編寫 VBA 代碼以將文件從一個共享點路徑移動到另一個共享點路徑。 移動文件后,我想在實際文件名的下一列中顯示“文件已移動”。 請在下面找到我的代碼。

    currRow = 11
On Error Resume Next
For currRow = currRow To LastRow

    Filename = ActiveSheet.Range("A" & currRow).Value
    From_Path = "blabla"
    To_Path = "blabla"
    Set FSO = CreateObject("Scripting.FileSystemObject")
        FSO.MoveFile From_Path & Filename, To_Path & Filename
        ActiveSheet.Range("B" & currRow) = "File Moved"
        ThisWorkbook.Save
    Next currRow

MsgBox "Files moved"

但問題是我正在為由於某些錯誤而未移動的文件獲取“文件已移動”。

感謝您在這方面的幫助。

你可以做這樣的事情。 最好限制On Error Resume Next這樣您就不會忽略其余代碼中的“意外”錯誤:

Dim msg
From_Path = "blabla"
To_Path = "blabla"
Set FSO = CreateObject("Scripting.FileSystemObject")

For currRow = currRow To LastRow

    Filename = ActiveSheet.Range("A" & currRow).Value
    On Error Resume Next        
    FSO.MoveFile From_Path & Filename, To_Path & Filename
    If Err.Number<>0 Then
        msg = "Error: " & Err.Description
    Else
        msg = "File moved"
    End If
    On Error Goto 0 'limit scope for ignoring errors...     
    ActiveSheet.Range("B" & currRow) = msg
    ThisWorkbook.Save
Next currRow

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM