VBA 如何初始化vCPath

VBA how to initialize vCPath

如何初始化 vCPath?

VBA Run-Time Error 1004

@Garry 的学生说我 "must somehow use the info you get from Application.FileDialog(msoFileDialogOpen) to generate the full filespec of the file you wish to open"。

最简单的方法是什么?我是 VBA 初学者:我已经编程 VBA 大约一个月了。

在你的原始代码中你有这个块:

' Open the file dialog
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = True
    .Show

'   Display paths of each file selected
    For lngCount = 1 To .SelectedItems.Count
    Next lngCount
    For Each strFilename In .SelectedItems
        MsgBox strFilename
    Next
End With

这已经是你想要的了。 strFilename 变量是用户选择的文件的路径和文件名。将其余代码放入另一个过程,然后调用新过程并将 strFilename 变量传递给它。

' Open the file dialog
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = True
    .Show

'   Display paths of each file selected
    For lngCount = 1 To .SelectedItems.Count
    Next lngCount
    For Each strFilename In .SelectedItems
        someSensibleProcName strFilename
    Next
End With

Private Sub someSensibleProcName (ByRef pathAndNameOfFile As String)

Dim sourceBook As Workbook

    ' Some of your code here

    Set sourceBook = Workbooks.OpenText (Filename:=pathAndNameOfFile _
        , Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
        xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
        , Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
        Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
        Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1)), _
        TrailingMinusNumbers:=True)

    ' The rest of your code here
    End Sub