使用Excel VBA打开csv

时间:2012-10-12 14:42:30

标签: vba excel-vba excel

我正在尝试使用此代码打开一个csv文件并且它一直给我一个“错误52错误的文件名或数字”

 Sub ShowFileDialog()
   Dim x As String
     Dim FF1 As Integer
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog( _
        msoFileDialogFilePicker)
    With dlgOpen

        .Show
    End With

x = CStr(dlgOpen.SelectedItems(1))
MsgBox x

Open x For Input As #FF1

Do While Not EOF(FF1)

Line Input #FF1, inputdata

Dim lineData() As String
lineData() = Split(inputdata, ",")



Loop
Close #FF1
End Sub

调试器突出显示Open for X行,但是我将它作为字符串

提供给它

1 个答案:

答案 0 :(得分:1)

直接在导致错误的行之前添加此行:

FF1 = FreeFile

因为Open For Input需要1到512之间的数字,这应该通过调用FreeFile获得。