Does anyone know what line or lines of code keep making my Excel crash every time I run the macro? When I comment out the copying from a different workbook and pasting into the current workbook, it runs fine, so I'm guessing it has something to do with those lines but I don't see why copying and pasting would cause it to crash...
The code is supposed to go through all the files in a folder that are specifically the .xlsb file type and copy a selection from that .xlsb file and paste it into the current .xlsm file.
Option Explicit Sub OpenFiles() Dim objFs As Object Dim objFolder As Object Dim file As Object Set objFs = CreateObject("Scripting.FileSystemObject") Set objFolder = objFs.GetFolder(Application.ThisWorkbook.Path) Dim lastCol As Integer lastCol = 2 For Each file In objFolder.Files If file Like "*.xlsb" Then Dim src As Workbook Set src = Workbooks.Open(file.Path, True, True) src.Worksheets("Rates").Range("C5", "C29").Copy ThisWorkbook.Worksheets("Sheet4").Cells(3, lastCol).PasteSpecial xlPasteValues src.Close False Set src = Nothing lastCol = lastCol + 1 End If Next End Sub
lastCol?