Vba 宏在PC上运行,但在MacOS上不运行:下标超出范围错误
我可以让这段代码在PC上运行,但不能在Mac上运行。该代码允许您选择文本文件并将其转换为工作表,然后将其附加到当前工作簿中Vba 宏在PC上运行,但在MacOS上不运行:下标超出范围错误,vba,macos,Vba,Macos,我可以让这段代码在PC上运行,但不能在Mac上运行。该代码允许您选择文本文件并将其转换为工作表,然后将其附加到当前工作簿中 Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x)) 上面的代码行将程序发送到错误处理程序,并导致下标超出范围错误 在下面的链接中,有一个Locals窗口的图片,显示了我想要获取的文件的路径名。 Application.GetOpenFilename和Select\u File\u或\u Files\u Mac都返
Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
上面的代码行将程序发送到错误处理程序,并导致下标超出范围错误
在下面的链接中,有一个Locals窗口的图片,显示了我想要获取的文件的路径名。
Application.GetOpenFilename和Select\u File\u或\u Files\u Mac都返回一个包含一个或多个文件名的数组,但第一个数组是基于一个的,并且您的Mac版本是基于零的
计数器x从1开始,因此对于只有一个文件(即FilesToOpen0)的mac阵列,它已经超出范围
您可以修改Mac代码以返回基于一的数组
修改此部分:
MySplit=SplitMyFiles,
ReDim将列表1返回到UBoundMySplit+1'基于一而不是基于零。。。
对于N=LBoundMySplit到UBoundMySplit
returnListN+1=MySplitN
下一个
……这一部分:
重拨返回列表1至1
returnList1=False
谢谢你。我不认为我会意识到这一点。我现在收到一条“找不到文件”错误消息。我现在就想办法解决这个问题。
Sub CombineTextFiles()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Dim answer As Integer
On Error GoTo ErrHandler
Application.ScreenUpdating = False
answer = MsgBox("Before moving forward, all other workbooks must be closed" _
& vbCrLf & "Do you wish to continue?", vbYesNo + vbQuestion)
If answer = vbYes Then 'do nothing
Else: Exit Sub
End If
sDelimiter = ","
#If Mac Then
FilesToOpen = Select_File_Or_Files_Mac()
#Else
FilesToOpen = Application.GetOpenFilename(fileFilter:="Text Files (*.txt), *.txt", _
MultiSelect:=True, Title:="Select the CDR Text Files to Open")
#End If
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
Set wkbAll = ActiveWorkbook
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:=","
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(FileName:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:=sDelimiter
End With
x = x + 1
Wend
wkbAll.Sheets.Copy After:=Workbooks(2).Sheets(Workbooks(2).Worksheets.Count)
wkbAll.Close False
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
Function Select_File_Or_Files_Mac() As String()
Dim MyPath As String
Dim MyScript As String
Dim MyFiles As String
Dim MySplit As Variant
Dim N As Long
Dim FName As String
Dim mybook As Workbook
On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")
'Or use MyPath = "Macintosh HD:Users:Ron:Desktop:TestFolder:"
' In the following statement, change true to false in the line "multiple
' selections allowed true" if you do not want to be able to select more
' than one file. Additionally, if you want to filter for multiple files, change
' {""com.microsoft.Excel.xls""} to
' {""com.microsoft.excel.xls"",""public.comma-separated-values-text""}
' if you want to filter on xls and csv files, for example.
MyScript = _
"set applescript's text item delimiters to "","" " & vbNewLine & _
"set theFiles to (choose file of type " & _
" {""com.microsoft.excel.xls"",""public.comma-separated-values-text"", ""public.text""} " & _
"with prompt ""Please select a file or files"" default location alias """ & _
MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
"set applescript's text item delimiters to """" " & vbNewLine & _
"return theFiles"
MyFiles = MacScript(MyScript)
Dim returnList() As String
On Error GoTo 0
If MyFiles <> "" Then
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'MsgBox MyFiles
MySplit = Split(MyFiles, ",")
ReDim returnList(LBound(MySplit) To UBound(MySplit))
For N = LBound(MySplit) To UBound(MySplit)
returnList(N) = MySplit(N)
Next N
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Select_File_Or_Files_Mac = returnList
Else
ReDim returnList(0 To 0)
returnList(0) = "False"
Select_File_Or_Files_Mac = returnList
End If
End Function