VBA Error Logging to tab on same Spreadsheet

SBest

New Member
Joined
Sep 7, 2021
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
I have a spreadsheet that copies a load of files, based on a file path in Column A and pastes them based on a file path in Column B.

In some cases, some of the file paths specified in Column A won't exist. At the moment these errors just come up as a message box. I want to add something to my vba so that these errors are outputted to a tab 'Errors' in the same workbook, with this list being cleared every time that the macro is run. I am struggling though!

This is the original vba

VBA Code:
Sub CopyFiles()
Dim src As String, dst As String, fl As String
Dim lr As Long
'Source directory
'Range("A2").Select
lr = Cells(Rows.Count, "C").End(xlUp).Row
For X = 2 To lr
src = Range("A" & X).Value
'Destination directory
dst = Range("B" & X).Value
'Filename
fl = Range("F" & X).Value
On Error Resume Next
'get project id
FileCopy src & "\" & fl, dst & "\" & fl
If Err.Number <> 0 Then
MsgBox "Copy error: " & src & "\" & fl
End If
Next X
On Error GoTo 0
MsgBox ("Complete!")
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I haven't tested it, but try something like this...change sheet5 references to whatever sheet number you want the errors logged on:

VBA Code:
Sub CopyFiles()
Dim src As String, dst As String, fl As String
Dim lr As Long
Dim lasterror As Integer

'clear errors
Sheet5.UsedRange.Delete
Set lasterror = 1

'Source directory
'Range("A2").Select
lr = Cells(Rows.Count, "C").End(xlUp).Row
For x = 2 To lr
src = Range("A" & x).Value
'Destination directory
dst = Range("B" & x).Value
'Filename
fl = Range("F" & x).Value
On Error Resume Next
'get project id
FileCopy src & "\" & fl, dst & "\" & fl
If Err.Number <> 0 Then

Sheet5.Range("A" & lasterror).Value = "Copy error: " & src & "\" & fl
lasterror = lasterror + 1

End If
Next x
On Error GoTo 0
MsgBox ("Complete!")
End Sub
 
Upvote 0
Here is an example of how to use an error handler to trap runtime errors and list them on a separate worksheet.
VBA Code:
'Example for logging runtime errors to separate worksheet
Sub ErrorLogDemo()
    Dim WB As Workbook
    Dim ErrorWS As Worksheet
    Dim ErrCnt As Long
    Dim CellRange As Range
    
    Set WB = ActiveWorkbook
    Application.ScreenUpdating = False
    
    'Delete any previous ErrorLog sheet
    On Error Resume Next
    Application.DisplayAlerts = False
    WB.Worksheets("ErrorLog").Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    DoEvents

    'Add ErrorLog sheet
    Set ErrorWS = WB.Worksheets.Add(After:=WB.Worksheets(Worksheets.Count))
    ErrorWS.Name = "ErrorLog"
    
    ErrorWS.Cells(1, 1).Value = "Error No."
    ErrorWS.Cells(1, 2).Value = "Error Desc."

    On Error GoTo ErrorLog

    'Create some test errors
    Debug.Print 100 / 0 'div by zero error
    Set CellRange = Range(100)
    WB = 12
    
    'Format result
    With ErrorWS
        .Range("A1:B1").Font.Bold = True
        .Range("A1:B1").Borders(xlEdgeBottom).Weight = xlMedium
        Range("A2").Select
        ActiveWindow.FreezePanes = True
        .Columns.AutoFit
    End With
    Application.ScreenUpdating = True
    Exit Sub

ErrorLog:
    ErrCnt = ErrCnt + 1
    ErrorWS.Cells(ErrCnt + 1, 1).Value = Err.Number
    ErrorWS.Cells(ErrCnt + 1, 2).Value = Err.Description
    Resume Next
End Sub
 
Upvote 0
I haven't tested it, but try something like this...change sheet5 references to whatever sheet number you want the errors logged on:

VBA Code:
Sub CopyFiles()
Dim src As String, dst As String, fl As String
Dim lr As Long
Dim lasterror As Integer

'clear errors
Sheet5.UsedRange.Delete
Set lasterror = 1

'Source directory
'Range("A2").Select
lr = Cells(Rows.Count, "C").End(xlUp).Row
For x = 2 To lr
src = Range("A" & x).Value
'Destination directory
dst = Range("B" & x).Value
'Filename
fl = Range("F" & x).Value
On Error Resume Next
'get project id
FileCopy src & "\" & fl, dst & "\" & fl
If Err.Number <> 0 Then

Sheet5.Range("A" & lasterror).Value = "Copy error: " & src & "\" & fl
lasterror = lasterror + 1

End If
Next x
On Error GoTo 0
MsgBox ("Complete!")
End Sub
Thanks - I am getting an 'object required' at @SeT lasterror = 1'. I have tried tweaking it but can't get it to run
 
Upvote 0
Is Sheet5 the actual sheet you're using? You need to "change sheet5 references to whatever sheet number you want the errors logged on"
 
Upvote 0
Then you need to make sure you have a sheet5 in your workbook. For example, in my sample workbook, I would need to reference Sheet2.
1697031950568.png
 
Upvote 0
Then you need to make sure you have a sheet5 in your workbook. For example, in my sample workbook, I would need to reference Sheet2.
View attachment 100158
Ah, I see what you mean. OK, it is now running without error, but I am getting nothing in the error sheet, even though I have some known non-valid file paths in my data
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top