excel 为什么这个VBA代码在PC上工作,而不是Mac?[duplicate]

lo8azlld  于 2022-11-18  发布在  Mac
关注(0)|答案(1)|浏览(132)

此问题在此处已有答案

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
58wvjzkj

58wvjzkj1#

谢谢白色先生。这就解决了这个问题。而且,令人惊讶的是,用“/files/”设置文件路径在PC和Mac上都有效。

Sub ExtractDataToDifferentSheets()

Dim totalpath As String
Dim sourceFiles As Workbook
Dim worksheetName As String
Dim totalpath_name As String
Dim rows, cols As Integer
Dim rowsNumber As Integer
Dim colsNumber As Integer

On Error GoTo HandleError

Application.ScreenUpdating = False

totalpath = Application.ActiveWorkbook.path & "/files/"

MyFile = Dir(totalpath)
    
Do While MyFile <> ""
    
    totalpath_name = totalpath & MyFile
    
    worksheetName = Replace(MyFile, ".xlsx", "")

    Sheets.Add.Name = worksheetName
    
    Set sourceFiles = Workbooks.Open(totalpath_name, True, True)
    
    rowsNumber = sourceFiles.Worksheets("sheet1").UsedRange.rows.Count
    
    colsNumber = sourceFiles.Worksheets("sheet1").UsedRange.Columns.Count

    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
    
    MyFile = Dir$
Loop

HandleError:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

相关问题