此问题在此处已有答案:
CreateObject("Scripting.FileSystemObject") doesn't work under Excel for Mac [duplicate](1个答案)
Is there an alternative to Scripting.FileSystemObject in Excel 2011 VBA for the mac?(2个答案)
昨天关门了。
没有错误。代码只是不运行。(现代Mac运行Ventura/Office 365)。代码读取子目录中的文件,并将它们粘贴到活动工作簿的新工作表中。我唯一的想法是路径错误,但我将“/files”设置为Mac友好...
Sub ExtractDataToDifferentSheets()
On Error GoTo HandleError
Application.ScreenUpdating = False
Dim objectFlieSys As Object
Dim objectGetFolder As Object
Dim file As Object
Dim totalpath As String
totalpath = Application.ActiveWorkbook.path & "/files"
Set objectFlieSys = CreateObject("Scripting.FileSystemObject")
Set objectGetFolder = objectFlieSys.GetFolder(totalpath)
Dim counter As Integer
counter = 1
For Each file In objectGetFolder.Files
Dim sourceFiles As Workbook
Dim worksheetName As String
worksheetName = file.Name
Sheets.Add.Name = worksheetName
Set sourceFiles = Workbooks.Open(file.path, True, True)
Dim rowsNumber As Integer
rowsNumber = sourceFiles.Worksheets("sheet1").UsedRange.rows.Count
Dim colsNumber As Integer
colsNumber = sourceFiles.Worksheets("sheet1").UsedRange.Columns.Count
Dim rows, cols As Integer
For rows = 1 To rowsNumber
For cols = 1 To colsNumber
Application.Workbooks(1).Worksheets(worksheetName).Cells(rows, cols) = sourceFiles.Worksheets("Sheet1").Cells(rows, cols)
Next cols
Next rows
rows = 0
sourceFiles.Close False
Set sourceFiles = Nothing
With ActiveWorkbook
.ActiveSheet.Name = worksheetName
counter = counter + 1
If counter > .Worksheets.Count Then
.Sheets.Add After:=.Worksheets(.Worksheets.Count)
End If
.Worksheets(counter).Activate
End With
Next
HandleError:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
1条答案
按热度按时间58wvjzkj1#
谢谢白色先生。这就解决了这个问题。而且,令人惊讶的是,用“/files/”设置文件路径在PC和Mac上都有效。