Excel Macros to import a .csv file into a workbook

ncomper
ncomper used Ask the Experts™
on
Hi,

I need help creating a macro that will import a .csv file into a workbook and refresh the pivot tables, is this possible and what would be the best method for achieving this?


The two files I've uploaded contain some of the raw data (File name: "Data to be imported") that needs to be loaded into the workbook automatically (File name: "Report Template").


Please advise.

Many Thanks,
Tom
Data-to-be-imported.csv
Report-Template.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

pls try something like this
Sub Macro3()

    Workbooks("Report-Template.xlsx").Activate
    Sheets("Raw Data").Activate
    With Sheets("Raw Data").QueryTables.Add(Connection:= _
        "TEXT;C:\myFolder\Data-to-be-imported.csv", Destination:=Range _
        ("$A$2")) ' change folder
        .Name = "Data-to-be-imported_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 2
        .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)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    ActiveWorkbook.RefreshAll
    Sheets("Report of Vulnerabilities").Activate
End Sub

Open in new window

Regards
Top Expert 2016
Commented:
and if you want to select the file on the fly then try
Sub Macro()

    Workbooks("Report-Template.xlsx").Activate
    Sheets("Raw Data").Activate
    With Sheets("Raw Data").QueryTables.Add(Connection:= _
        "TEXT;" & GetFile, Destination:=Range _
        ("$A$2")) ' change folder
        .Name = "Data-to-be-imported_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 2
        .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)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    ActiveWorkbook.RefreshAll
    Sheets("Report of Vulnerabilities").Activate
End Sub
Function GetFile() As String
Dim filename__path As Variant
filename__path = Application.GetOpenFilename(FileFilter:="Csv (*.CSV), *.CSV", Title:="Select File To Be Opened")
If filename__path = False Then Exit Function
GetFile = filename__path
End Function

Open in new window

Author

Commented:
Hi Again,

Thanks for this!

I've spent some time playing around with the above macro today and the only issue had is when it comes to the Line Breaks in the imported data. This causes a single line of data to be spread across additional rows, I've looked around the web and cannot find a solution to this, any recommendations on where to look?

Many Thanks,

Author

Commented:
Thanks for your help here, script worked perfectly

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial