簡體   English   中英

使用VBA腳本將CSV解析為XLS時出錯

[英]Error parsing CSV to XLS with VBA script

此功能旨在打開CSV文件,並將所有數據導入Excel工作簿上的特定工作表。

但是我認為在某些情況下使用分隔符無法解決某些問題。

這是我的功能:

Sub LoopAllExcelFilesInFolder_Invenotry()

  Dim strFilename As String
  Dim wsMstr  As Worksheet:   Set wsMstr = ThisWorkbook.Sheets("ALL_ACTIUS")

  If MsgBox("Erase sheet before start importing?", vbYesNo, "Delete?") _
= vbYes Then wsMstr.UsedRange.Clear

  strFilename = Application.GetOpenFilename _
                (FileFilter:="CSV File (*.csv), *.csv", _
                Title:="Select CSV file: ")

 Worksheets("ALL_MACHINES").Activate
 With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & strFilename, _
    Destination:=Range("A1"))
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 1252
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = True
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
 End With

End Sub

這是源代碼(CSV文件)的一部分,包含不按預期轉換的行:

在此處輸入圖片說明

在這里,您可以看到如何將此行轉換為Excel。 12X5“字符串不應在那里。第一列應僅包含IP地址。

在此處輸入圖片說明

我敢肯定這是一個分隔符問題,但是我找不到解決方案。

Workbooks.Open命令為我解決了該問題。

Dim wb As Workbook

strFilename = "yourfilename.csv"

Set wb = Workbooks.Open(Filename:=strFilename, local:=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM