Is there a way to import data from .csv to active excel sheet?

前端 未结 4 2107
无人及你
无人及你 2020-12-05 19:40

I have a csv file always named the same, called SO2PO.csv. It has data that I import into an excell sheet called PO Data, in a workbook called Open Order. I need to find a w

4条回答
  •  不知归路
    2020-12-05 19:56

    Add this code to create a QueryTable in the PO Data sheet to your data source

    Once you have created the QueryTable you can then just right click Refresh the data (or refresh on open)

    Sub CSV_Import()
    Dim ws As Worksheet, strFile As String
    
    Set ws = ActiveWorkbook.Sheets("PO Data") 'set to current worksheet name
    
    strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
    
    With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
         .TextFileParseType = xlDelimited
         .TextFileCommaDelimiter = True
         .Refresh
    End With
    End Sub
    

提交回复
热议问题