我在VBA中创建了一个.dvb文件,出现编译错误,指出:
未定义变量
有两个代码模块,一个用于表单,一个用于公共对话框。我收到的错误是关于常见对话框代码的,该对话框代码已从其他位置清除以处理浏览和添加工程图。
常用对话框代码模块摘录:
Public Function GetFiles( _
ByVal sInitFolder As String, _
ByVal sTitle As String, _
ByVal sFilter As String, _
ByVal nFilterIndex As Integer) As String()
strReturn = FileBrowseOpen(sInitFolder, sTitle, sFilter, nFilterIndex, True) 'code failing here "compile error variable not defined"
GetFiles = Split(strReturn, ",")
End Function
Form Code模块摘录:
Private Sub cmdAddDwg_Click()
Dim initFolder As String
Dim filter As String
Dim fileNames() As String
Dim i As Integer
initFolder = ThisDrawing.Path
filter = "AutoCAD Drawing Files (*.dwg)|*.dwg|All Files (*.*)|*.*"
fileNames = GetFiles(initFolder, "Select Drawing Files", filter, 0)
If UBound(fileNames) > 0 Then
For i = 1 To UBound(fileNames)
lstDwgList.AddItem fileNames(0) & "\" & fileNames(i)
Next
End If
End Sub
我的VBA知识充其量是有限的。
非常感谢。
答案 0 :(得分:1)
将变量strReturn
定义为字符串:
Public Function GetFiles ( _
ByVal sInitFolder As String, _
ByVal sTitle As String, _
ByVal sFilter As String, _
ByVal nFilterIndex As Integer) As String()
Dim strReturn As String ' This line was missing
strReturn = FileBrowseOpen(sInitFolder, sTitle, sFilter, nFilterIndex, True)
GetFiles = Split(strReturn, ",")
End Function
或重写代码的该部分以完全忽略strReturn
变量:
Public Function GetFiles ( _
ByVal sInitFolder As String, _
ByVal sTitle As String, _
ByVal sFilter As String, _
ByVal nFilterIndex As Integer) As String()
GetFiles = Split(FileBrowseOpen(sInitFolder, sTitle, sFilter, nFilterIndex, True), ",")
End Function
答案 1 :(得分:0)
Private Sub cmdAddDwg_Click()
Dim initFolder As String
Dim filter As String
Dim fileNames() As String
Dim i As Integer
'initFolder = ThisDrawing.Path 'gets dwg folder path
initFolder = lastPath 'gets last known location
filter = "AutoCAD Drawing Files (*.dwg)|*.dwg|All Files (*.*)|*.*"
fileNames = GetFiles(initFolder, "Select Drawing Files", filter, 0)
If UBound(fileNames) > 0 Then
For i = 1 To UBound(fileNames)
lstDwgList.AddItem fileNames(0) & "\" & fileNames(i)
Next
End If
结束子