Excel VBA,用于使用查询

时间:2018-08-05 16:10:48

标签: excel vba csv import

我下面有以下代码用于导入csv文件。我如何修改它以仅导入csv文件的选定列。 (仅说第1,3和5列)?

我的确切要求是仅在使用“ QueryTables.Add”时如何选择特定列

请帮助。我录制了要导入CSV的宏。但我想浏览要导入的文件。下面的代码要求文件并且可以完美工作。导入文件后,只有我必须删除不需要的列(vba)。

Sub ImportEvents()
    Sheets("Import").Unprotect Password:="2484"
    Dim xFileName As Variant
    xFileName = Application.GetOpenFilename("CSV File (*.csv), *.csv", , "Browse the File S2KEventMsg_Table.csv", , False)
    If xFileName = False Then Exit Sub
    On Error Resume Next
    Application.ScreenUpdating = False
    Sheets("Import").Select
    Dim lastRowToDel As Long
    lastRowToDel = Range("B" & Rows.Count).End(xlUp).Row
    Range("A1:E" & lastRowToDel).Select
    Selection.ClearContents
    Range("A2").Select
    With ActiveSheet.QueryTables.Add("TEXT;" & xFileName, Range("A2"))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 936
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
        Columns("P:AD").Select
        Selection.Delete Shift:=xlToLeft
        Columns("K:N").Select
        Selection.Delete Shift:=xlToLeft
        Columns("D:H").Select
        Selection.Delete Shift:=xlToLeft
        Columns("B:B").Select
        Selection.Delete Shift:=xlToLeft
        Columns("B:B").Select
        Selection.NumberFormat = "dd/mm/yyyy hh:mm:ss.000"
        Columns("A:A").ColumnWidth = 50
        Columns("C:C").ColumnWidth = 9
        Columns("D:D").ColumnWidth = 40
        Range("C:C").Select
        With Selection
        .HorizontalAlignment = xlCenter
        End With
        Range("A:E").Select
        With Selection
        .WrapText = True
        End With
    End With
    Range("A1").Formula = "Station | Voltage Level | Equipment"
    Range("B1").Formula = "Date | Time"
    Range("C1").Formula = "Severity"
    Range("D1").Formula = "Event State"
    Range("E1").Formula = "User"
    Range("A1:E1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .Font.Bold = True
    Range("A1").Select
    Sheets("Import").Protect Password:="2484"
    Application.ScreenUpdating = True
    MsgBox "Importing of Events Successful" & vbNewLine & "Goto STEP 2 or STEP 3", , "Import Successful"
    Sheets("Start").Select
    Sheets("Start").Range("H14").Select
    End With



End Sub

1 个答案:

答案 0 :(得分:0)

一个问题是您尚未包括正在使用的数据的样本;另一个是您的代码不完整。 (请参阅How to create a Minimal, Complete, and Verifiable example。)

有几种将CSV文件导入Excel的方法,包括指定所需的列。

似乎您是通过使用Developer> Record Macro 然后是Data> From Text/CSV 来创建该代码的

生成所需代码的最简单方法是再次执行,但是这次 Edit 导入查询并删除您不需要的列不想。

img

要选择列,只需右键单击不需要的列标题,然后单击 Remove Columns ,否则选择所有想要的列,右键单击列标题,然后单击 Remove Other Columns

img

当您按需要对列进行布局后,单击 Close & Load 将数据作为表格返回。

这时,您可以 Stop Recording 宏并根据需要编辑代码。