【发布时间】:2012-07-19 01:52:54
【问题描述】:
我最近从 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
【问题讨论】:
-
“当我尝试在 Mac 上运行它时,它返回一个错误”......那个错误会是......?
-
我得到的错误是“编译错误:变量未定义”
-
上面代码中的哪一行表示错误?
-
我在第 31 行得到它
msoFileDialogViewList.. -
Mac 示例请参见此处:rondebruin.nl/mac.htm