Search code examples
vbaexcelexcel-2007

EXCEL 2007: macro runs on one PC but not on other


I have written a macro for my boss to open a particular folder which contain approximately 100 workbooks having same format and collate all the data from those workbooks into the host excel where the macro is. Now the problem is, it works absolutely fine on my PC but when I had run it on the boss' PC it runs without executing the code(no data is collated) and displays the success message in the end in a second. Any help is appreciated. Here is the macro code

Sub collate()

Application.ScreenUpdating = False

Dim folderDialog As FileDialog
Dim folderPath As String, filename As String
Dim temp As Variant
Dim folder As Object, file As Object
Dim row As Integer, lastrow As Integer

MsgBox "Please select the folder containing all the input files", vbOKOnly

Set folderDialog = Application.FileDialog(msoFileDialogFolderPicker)
folderDialog.AllowMultiSelect = False
folderDialog.Show

On Error GoTo ext
folderPath = folderDialog.SelectedItems(1)

Set temp = CreateObject("Scripting.FileSystemObject")
Set folder = temp.GetFolder(folderPath)
row = Sheet1.Cells(Rows.Count, 2).End(xlUp).row
If row > 3 Then Sheet1.Range("B4:I" & row).Clear
row = 4

For Each file In folder.Files

    filename = file.Name
    filename = Left(filename, Len(filename) - 5)

    Application.Workbooks.Open (folderPath & "\" & filename)
    lastrow = Workbooks(filename).Worksheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).row
    Workbooks(filename).Worksheets("Sheet1").Range("B4:I" & lastrow).Copy

    Sheet1.Range("B" & row).PasteSpecial xlPasteValues
    Sheet1.Range("B" & row).PasteSpecial xlPasteFormats
    Application.CutCopyMode = False

    row = Sheet1.Cells(Rows.Count, 2).End(xlUp).row + 1
    Application.Workbooks(filename).Close savechanges:=False

Next

ext:
If folderPath = "" Then
MsgBox "Folder not selected!"
Application.ScreenUpdating = True
Exit Sub
End If

Sheet1.Range("A1").Select
Application.ScreenUpdating = True
MsgBox "Data successfully merged!", vbInformation
End Sub 

Solution

  • You may need to enable the Microsoft Scripting Runtime library on your boss's computer if you haven't already. In some instances this library needs to be enabled in order to interface with the File System Object.

    This library can be accessed from the Visual Basic Editor by pressing Tools > References > Microsoft Scripting Runtime. See the link below for further information.

    Microsoft Scripting Runtime Library