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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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