csv import中多个变量的错误

时间:2014-05-14 10:22:43

标签: excel excel-vba csv vba

在学习了VBE基础知识后,我终于尝试构建一个用于打开csv文件的宏,让它放在一个excell表中,保存为csvname和redo,直到完成所有操作。

在输入csv文件后,我遇到了问题。它给出了错误号13,我绝对不知道如何解决它。我目前认为你不能把变量放在那里?我对吗?有没有解决方案?

请举例说明如何解决此问题

Sub CSVnaarxlsx()
    'On Error Resume Next
    'declareer variabelen
    Dim strpath As String
    Dim fmn As Integer
    Dim lmn As Integer
    Dim csvname As String
    'active workbook pathway
    strpath = Application.ActiveWorkbook.Path
    'ask user for first and last number
    fmn = InputBox("first mouse number")
    lmn = InputBox("last mouse number")
    'einde sub if inputbox is empty
    'If fmn = "" Then
    'MsgBox "No first mouse number"
    'Exit Sub
    'End If
    'If lmn = "" Then
    'MsgBox "No Last mouse number"
    'Exit Sub
    'End If

'assign variables

'loop al de files
 For fmn = fmn To (lmn + 1)
 csvname = "m" & fmn
 'input of csv file
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;strpath & " / " & csvname" & ".csv""" _
        , Destination:=Range("sheet1!$A$1"))
        .Name = csvname
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
        , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1)
        .TextFileDecimalSeparator = "."
        .TextFileThousandsSeparator = ","
        .TextFileTrailingMinusNumbers = True
    End With
Call CsvToXlsx(ByVal csvname, strpath)
Next fmn

End Sub

Sub CsvToXlsx(ByVal csvname, strpath)
ChDir (strpath & "/verwerkt")
csvname = csvname & .xlsx
  ActiveWorkbook.SaveAs Filename:=csvname, FileFormat:=51
End Sub

1 个答案:

答案 0 :(得分:0)

下面的例程提示用户选择CSV并将它们全部保存为.xlsx文件到与主机工作簿相同的目录中。我添加了很多评论来解释发生的一切 - 希望这有帮助!

Option Explicit
Sub OpenCSVFiles()

Dim Path As String
Dim TargetFiles As FileDialog
Dim Index As Long
Dim CSVBook As Workbook

'prompt user to select CSV files
Set TargetFiles = Application.FileDialog(msoFileDialogOpen)
With TargetFiles
    .AllowMultiSelect = True
    .Title = "Select CSV files:"
    .ButtonName = ""
    .Filters.Clear
    .Filters.Add ".csv files", "*.csv"
    .Show
End With

'check to see if the user clicked cancel, if so exit
If TargetFiles.SelectedItems.Count = 0 Then Exit Sub

'loop through the selected files
For Index = 1 To TargetFiles.SelectedItems.Count

    'open the CSV
    Set CSVBook = Workbooks.Open(TargetFiles.SelectedItems(Index))

    'extract the file path and file name
    Path = CSVBook.FullName
    Path = Left(Path, Len(Path) - 4) '<~ remove the .csv extension

    'save as an xlsx file, no alerts
    Application.DisplayAlerts = False
    CSVBook.SaveAs Filename:=Path, FileFormat:=51 '<~ .xlsx = 51 on Windows
    Application.DisplayAlerts = True

    'close the CSV and repeat
    CSVBook.Close False

Next Index

End Sub