简体   繁体   中英

Excel VBA script not running on Excel 2016 with FileSystemObject

I've never used VB(A) before, so forgive me if this is a trivial question.

I am trying to run the code outlined here on Excel 2016 on a Mac.

Sub simpleXlsMerger()
Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
'change folder path of excel files here
Set dirObj = mergeObj.Getfolder("/...filepath")
Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
'change "A2" with cell reference of start point for every files here
'for example "B3:IV" to merge all files start from columns B and rows 3
'If you're files using more than IV column, change it to the latest column
'Also change "A" column on "A65536" to the same column as start point
Range("A2:IV" & Range("A65536").End(xlUp).Row).Copy
ThisWorkbook.Worksheets(1).Activate
'Do not change the following column. It's not the same column as above
Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
End Sub

However, I get the error, that goes off without specifying a line:

在此处输入图片说明

Any thoughts on how I can modify this code for Mac?

Try this, based on the other (unaccepted) answer to similar question.

Is there an alternative to Scripting.FileSystemObject in Excel 2011 VBA for the mac?

The problem is that Scripting.Runtime library is not available on Mac OS, so you can't do CreateObject("Scripting.FileSystemObject") . This uses the VBA Dir function to build a Collection of files.

Also revised for more efficient "copy" that doesn't use the Copy method.

(untested, so bear with me in case of typos/etc.)

Sub simpleXlsMerger()
Dim bookList As Workbook, vals as Variant
Dim filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
'change folder path of excel files here
Set filesObj = GetFileList("/...filepath")
For Each everyObj In filesObj
    Set bookList = Workbooks.Open(everyObj)
    'change "A2" with cell reference of start point for every files here
    'for example "B3:IV" to merge all files start from columns B and rows 3
    'If you're files using more than IV column, change it to the latest column
    'Also change "A" column on "A65536" to the same column as start point
    vals = Range("A2:IV" & Range("A" & Rows.Count).End(xlUp).Row).Value2

    With ThisWorkbook.Worksheets(1)
    'Do not change the following column. It's not the same column as above
        .Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).Resize(UBound(vals, 1), UBound(vals, 2)).Value = vals
    End With

    bookList.Close
Next
End Sub

Function GetFileList(folderPath As String) As Collection
'mac vba does not support wildcards in DIR function

    Dim file As String
    Dim returnCollection As New Collection

    If Right$(folderPath, 1) <> "\" Then
        folderPath = folderPath & "\"
    End If

    file = Dir$(folderPath) 'setup initial file

    Do While Len(file)
        returnCollection.Add folderPath & file
        file = Dir$
    Loop

    Set GetFileList = returnCollection
End Function

In the VBA window, click tools then add reference. Make sure Microsoft Office Object Library (14 or 16) is checked. You will need this for the automation.

An alternative idea would be to create a loop and save each excel file as a csv then run a batch script.

Example: copy *.csv merged.csv

For the CSV loop use: Converting XLS/XLSX files in a folder to CSV

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM