传输特定于Windows的宏以在Mac Excel上运行

时间:2012-07-19 19:56:25

标签: windows excel macos excel-vba vba

我最近从PC改为Mac。我运行了很多宏,其中99%运行正常,但我有一台在Mac上不能运行。

它在文件中的所有工作簿中运行一组其他宏。为此,它使用如下字符串:

Function BrowseFolder(Title As String, _ Optional InitialFolder As String = vbNullString, _ Optional InitialView As Office.MsoFileDialogView = _ msoFileDialogViewList) As String

当我尝试在Mac上运行它时会返回错误:

  

“编译错误:未定义变量”

我想知道是否有人可以帮我将这个宏移植到Mac上运行。它是在Excel 2007 Windows上构建的,我正在尝试在Excel 2011 Mac上运行它。

Option Explicit

Function GetFolder(Optional strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
     If Not IsEmpty(strPath) Then
        .InitialFileName = strPath
    End If
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
End With
NextCode:
 GetFolder = sItem
Set fldr = Nothing
End Function

Private Sub test()
Dim v As Variant

'V = GetFolder()
v = BrowseFolder("Select folder")
End Sub

Function BrowseFolder(Title As String, _
        Optional InitialFolder As String = vbNullString, _
        Optional InitialView As Office.MsoFileDialogView = _
            msoFileDialogViewList) As String
    Dim v As Variant
    Dim InitFolder As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = Title
        .InitialView = InitialView
        If Len(InitialFolder) > 0 Then
            If Dir(InitialFolder, vbDirectory) <> vbNullString Then
                InitFolder = InitialFolder
                If Right(InitFolder, 1) <> "\" Then
                    InitFolder = InitFolder & "\"
                End If
                .InitialFileName = InitFolder
            End If
        End If
        .Show
        On Error Resume Next
            Err.Clear
            v = .SelectedItems(1)
            If Err.Number <> 0 Then
                v = vbNullString
            End If
        End With
    BrowseFolder = CStr(v)
End Function

1 个答案:

答案 0 :(得分:1)

msoFileDialogViewList是指Windows标准文件对话框的特定视图。 Mac标准文件对话框没有等效模式;我的猜测是InitialView参数在Mac平台上不存在或被忽略。

我建议完全删除参数或使用等效的整数值(1)而不是符号名称。