【问题标题】:import csv in excel在excel中导入csv
【发布时间】:2014-04-23 08:34:00
【问题描述】:

5 月 14 日编辑

经过大量阅读,我终于了解了 VBA 的基础知识。我在下面创建了宏,但它仍然无法正常工作,它不会插入 csv 文件。 此宏完成后,保存的文件全部为空。使用 debug.print 我确认文件的字符串是完整的,但仍然缺少一些东西?

谁能帮我解决这个问题

提前致谢

Sub CSVimporterennaarxlsx()
    'On Error Resume Next
    'declare variable
    Application.ScreenUpdating = False
    Dim strpath As String
    Dim fmn As Integer
    Dim lmn As Integer
    Dim csvname As String
    Dim strpathcsvname 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 all the files
     For fmn = fmn To lmn
     csvname = "m" & fmn
     strpathcsvname = strpath & "\" & csvname & ".csv"
     'input of csv file
'        ActiveSheet.Cells.Delete

        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" + strpathcsvname, _
            Destination:=Range(A1))
'filename without extension
            .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
Application.DisplayAlerts = True
    End Sub

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

    End Sub

【问题讨论】:

标签: excel vba


【解决方案1】:

尝试打开.csv 文件并将其保存为.xls 文件

Sub CsvToXls (csvname)
  Workbooks.Open Filename:=csvname
  xlsname = Replace(csvname, ".csv",".xls")  
  ActiveWorkbook.SaveAs Filename:=xlsname , FileFormat:=xlNormal
End Sub

然后,迭代一个目录中的所有.csv 文件

Sub AllCsvToXls(dirname)        
    Dim csv As Variant 
    csv = Dir(dirname & "\*.csv")
    While (csv <> "")
      CsvToXls (dirname & "\" & csv)
      csv = Dir
    Wend  
End Sub

最后,调用它...

AllCsvToXls(ThisWorkbook.Path)

【讨论】:

  • +1 非常干净的代码。我建议使用 .OpenText 方法,因为 OP 将 True 设置为 CommaTab 分隔符。
  • 非常感谢这么快的回答,有没有可以找到代码解释的来源。
  • 代码(在我看来这显然是有偏见的,因为我自己编写了代码)非常可读和易于理解。如果你对它使用的基本编程结构(SubWhile)和库函数(DirReplace)有疑问,我建议你找一本关于 VBA 的好书开始学习。
  • 什么是 su ?我认为这是另一个论坛?我已经为 VBA 买了一本书,所以我希望我能在大约一周左右的时间内自己解决这个问题。
  • SU = 超级用户 (http:\\superuser.com)
猜你喜欢
  • 1970-01-01
  • 2012-08-06
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-27
相关资源
最近更新 更多