Macro coming up with runtime error 13 and not closing a workbook

ghrek

Active Member
Joined
Jul 29, 2005
Messages
427
The macro is supposed to open and copy the data from onr workbook to another.

The issue im having is its not closing what I think is the last workbook and says backup file not updated and also get runtime error 13. Any ideas?
VBA Code:
Sub mergefiles()
Dim Notdone As String
Dim allSheets As String
Dim file As Object
Dim sht As Worksheet
Dim fso As Object
Set fso = CreateObject("new:{0D43FE01-F093-11CF-8940-00A0C9054228}") ' new fileSystemObject

Dim tgtWb As Workbook, srcWb As Workbook
Set tgtWb = ThisWorkbook
Dim ix As Long
For ix = 1 To tgtWb.Sheets.Count
    allSheets = allSheets & "<" & tgtWb.Sheets(ix).Name & ">"
Next ix

For Each file In fso.GetFolder("c:\deletenow\").Files
    If Not LCase(file) Like "*ergeall*" And Left(file, 1) <> "~" Then
        On Error Resume Next
        Call waitForAnyUnlock(file)
        If Err <> 0 Then
            Notdone = Notdone & vbLf & file.Name
        Else
            On Error GoTo 0
            
            Set srcWb = Workbooks.Open(file)
            For Each sht In srcWb.Sheets
                If InStr(1, allSheets, "<" & sht.Name & ">", 1) > 0 Then
                    sht.UsedRange.Copy
                    With tgtWb.Sheets(sht.Name)
                        .Cells(.UsedRange.Rows.Count + .UsedRange.Row, 2).PasteSpecial xlPasteValues
                    End With
                   tgtWb.Sheets(sht.Name).Activate
                   Selection.Columns(1).Offset(0, -1) = srcWb.Name
                End If
            Next
            srcWb.Close False
        End If
    End If
Next
Sheets("Instructions").Activate

MsgBox "Merge complete " & IIf(Notdone = "", ".", _
    " except the following files were skipped because they were open" & Notdone)


End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Runtime error 13, I think is for an invalid value for a variable type. Can you step through the code (F8) to find out exactly which line is causing you trouble?
 
Upvote 0
It appears to be here.

1648493677416.png
 
Upvote 0
Can you try changing all instances of:
srcWb.Sheets
to
srcWb.worksheets

same for tgtWb.sheets

See if that makes a difference
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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