我的 VBA 代码只能在 Windows 上运行,但不能在 Mac 系统上运行
My VBA code only runs on Windows but not on a Mac system
当我在 Windows 计算机上 运行 时,此代码有效,但是当我在 Mac 计算机上 运行 时,我收到此错误:
Run-time error: '1004' Sorry we couldn't find file.xlsx. Is it possible it was moved, renamed or deleted?
我确认文件 'file.xlsx' 可用。有没有办法修改我的代码以使其在 Windows 和 Mac 上都起作用?
Sub CollectXLSXData()
' To loop through all .xslx files in a system specified folder and perform a set task on them
Application.ScreenUpdating = False
Dim MergeFileNames As String
Dim MergeFile As Workbook
Dim ws As Worksheet
Dim SourceRange As Range
Dim Data As Worksheet
Set Data= ThisWorkbook.Worksheets("Data")
ChDir ThisWorkbook.Path
MergeFileNames = Dir("*.xlsx")
Do Until MergeFileNames = ""
Set MergeFile = Workbooks.Open(MergeFileNames)
For Each ws In MergeFile.Worksheets
Set SourceRange = ws.Range("A1").CurrentRegion
SourceRange.Offset(1, 0).Resize(SourceRange.Rows.Count - 1).Copy _
Data.Cells(MTIData.Rows.Count, 1).End(xlUp).Offset(1, 0)
Next ws
Application.CutCopyMode = False
MergeFile.Close
MergeFileNames = Dir
Loop
Application.ScreenUpdating = True
End Sub
为了不必处理通配符与 MAC 系统的不兼容问题,我继续引用代码以从保存在特定文件夹中的所有文件中获取数据。该文件夹可以包含任何 excel 文件格式。现在我的代码在 Windows 和 MAC 环境中运行都没有问题。
Sub CollectFiles()
' To loop through all files in a system specified folder and copy to a master file/sheet
Dim FolderPath As String
Dim Files As String
Dim MergeFile As Workbook
Dim ws As Worksheet
Dim SourceRange As Range
Dim Data As Worksheet
Set Data = ThisWorkbook.Worksheets("Data")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
FolderPath = ThisWorkbook.Path & "/xFiles/"
Files = Dir(FolderPath)
Do Until Files = ""
Set MergeFile = Workbooks.Open(FolderPath & Files)
For Each ws In MergeFile.Worksheets
Set SourceRange = ws.Range("A1").CurrentRegion
SourceRange.Offset(1, 0).Resize(SourceRange.Rows.Count - 1).Copy _
Data.Cells(Data.Rows.Count, 1).End(xlUp).Offset(1, 0)
Next ws
Application.CutCopyMode = False
MergeFile.Close
Files = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
当我在 Windows 计算机上 运行 时,此代码有效,但是当我在 Mac 计算机上 运行 时,我收到此错误:
Run-time error: '1004' Sorry we couldn't find file.xlsx. Is it possible it was moved, renamed or deleted?
我确认文件 'file.xlsx' 可用。有没有办法修改我的代码以使其在 Windows 和 Mac 上都起作用?
Sub CollectXLSXData()
' To loop through all .xslx files in a system specified folder and perform a set task on them
Application.ScreenUpdating = False
Dim MergeFileNames As String
Dim MergeFile As Workbook
Dim ws As Worksheet
Dim SourceRange As Range
Dim Data As Worksheet
Set Data= ThisWorkbook.Worksheets("Data")
ChDir ThisWorkbook.Path
MergeFileNames = Dir("*.xlsx")
Do Until MergeFileNames = ""
Set MergeFile = Workbooks.Open(MergeFileNames)
For Each ws In MergeFile.Worksheets
Set SourceRange = ws.Range("A1").CurrentRegion
SourceRange.Offset(1, 0).Resize(SourceRange.Rows.Count - 1).Copy _
Data.Cells(MTIData.Rows.Count, 1).End(xlUp).Offset(1, 0)
Next ws
Application.CutCopyMode = False
MergeFile.Close
MergeFileNames = Dir
Loop
Application.ScreenUpdating = True
End Sub
为了不必处理通配符与 MAC 系统的不兼容问题,我继续引用代码以从保存在特定文件夹中的所有文件中获取数据。该文件夹可以包含任何 excel 文件格式。现在我的代码在 Windows 和 MAC 环境中运行都没有问题。
Sub CollectFiles()
' To loop through all files in a system specified folder and copy to a master file/sheet
Dim FolderPath As String
Dim Files As String
Dim MergeFile As Workbook
Dim ws As Worksheet
Dim SourceRange As Range
Dim Data As Worksheet
Set Data = ThisWorkbook.Worksheets("Data")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
FolderPath = ThisWorkbook.Path & "/xFiles/"
Files = Dir(FolderPath)
Do Until Files = ""
Set MergeFile = Workbooks.Open(FolderPath & Files)
For Each ws In MergeFile.Worksheets
Set SourceRange = ws.Range("A1").CurrentRegion
SourceRange.Offset(1, 0).Resize(SourceRange.Rows.Count - 1).Copy _
Data.Cells(Data.Rows.Count, 1).End(xlUp).Offset(1, 0)
Next ws
Application.CutCopyMode = False
MergeFile.Close
Files = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub