Excel Macros to import a .csv file into a workbook

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
LVL 5
ncomperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
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
0
Rgonzo1971Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ncomperAuthor 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,
0
ncomperAuthor Commented:
Thanks for your help here, script worked perfectly
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.