将数据从Excel中的外部CSV追加到表中 [英] append data to table from external csv in excel

查看:68
本文介绍了将数据从Excel中的外部CSV追加到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在excel文件中有一个包含数据的表,如何使用从csv获取外部数据"将数据附加到该表.我的表格的最后一个单元格位于A3016中,然后我突出显示了A3017单元格,并从该单元格中选择了获取外部数据",该数据已导入,但不像我手动将数据添加到下一个空闲单元格时那样属于该表.

If I have a table with data in a excel file, how can I append data to that table using the "get external data - from csv". My table last cell is in A3016, I then highlighted the A3017 cell and from that cell choosed "Get external data", the data is imported but it does not belong to the table like it does if I manually add data to the next free cell.

推荐答案

您可以尝试添加以下VBA代码以获得所需的结果:

You can try adding this VBA code to achieve the desired results:

Sub Append_CSV_File()

    Dim csvFileName As Variant
    Dim destCell As Range

    Set destCell = Worksheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1)      'CHANGE SHEET NAME

    csvFileName = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv", Title:="Select a CSV File", MultiSelect:=False)
    If csvFileName = False Then Exit Sub

    With destCell.Parent.QueryTables.Add(Connection:="TEXT;" & csvFileName, Destination:=destCell)
        .TextFileStartRow = 2
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .Refresh BackgroundQuery:=False
    End With

    destCell.Parent.QueryTables(1).Delete
    End Sub

让我知道这是否适合您.

let me know if that works for you.

这篇关于将数据从Excel中的外部CSV追加到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆