Seeking assistance with run time error 432

auto.pilot

Well-known Member
Joined
Sep 27, 2007
Messages
734
Office Version
  1. 365
Platform
  1. Windows
I have the following code, which simply opens workbooks that are all in the same folder (this is the source). the code then copies a range on the General worksheet within each of the workbooks. The range is then pasted to the destination workbook (the one with this code). All of the workbooks in the source folder are macro enabled as is the destination workbook.

There are 66 workbooks in the source folder. This code works perfectly for the first 58 source workbooks, but hangs on the next one. I can't see any reason for the error, which is "File name or class name not found during Automation operation". I verified that each workbook has a sheet named General.

how can I fix this?

Thanks in advance.

Jim



Code:
Option Explicit
Const SOURCE_FOLDER = "\\server\Files\Clients\Portfolios\"


Sub AggregateDataFromFiles()
          
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Dim fs As Object
    Dim objFolder As Object
    Dim objFolderName As String
    objFolderName = SOURCE_FOLDER
    Dim filePath As String
    Dim objFile As Object
    Dim targetWb As Workbook
    Dim lastrow As Long
    Dim TopRow As Long
    Dim BottomRow As Long
    Cells.ClearContents
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fs.GetFolder(objFolderName)
    For Each objFile In objFolder.Files
        
        filePath = objFolderName & "\" & objFile.Name
        Set targetWb = GetObject(filePath)
        
        targetWb.Worksheets("General").AutoFilterMode = False
        
        targetWb.Worksheets("General").Range("A3:AX1000").Copy
        
        lastrow = Range("C" & Rows.Count).End(xlUp).Row
        ActiveSheet.Range("B" & lastrow + 1).PasteSpecial xlPasteValues
        Application.CutCopyMode = False

        targetWb.Close (False)
  
    Next
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
End Sub
 
Last edited:
when you debug, is the value passed into the open function a valid file name and the file is closed? Maybe it is a problem with memory but you are closing the workbooks so that should free memory each loop.

Try running the macro with only that 57th book in the folder so it processes it first and see what happens.
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
when you debug, is the value passed into the open function a valid file name and the file is closed? Maybe it is a problem with memory but you are closing the workbooks so that should free memory each loop.

Try running the macro with only that 57th book in the folder so it processes it first and see what happens.

Thanks again for all your assistance. The issue appears to be related to memory. I created two new 'Source' folders, then put 33 workbooks in each one. Thereafter, I was able to run the code separately for both folders and everything worked.

Thanks

jim
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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