Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-27-2017, 03:41 PM
Alex99 Alex99 is offline Merging multiple CSV files into a new EXCEL workbook Windows 7 64bit Merging multiple CSV files into a new EXCEL workbook Office 2007
Novice
Merging multiple CSV files into a new EXCEL workbook
 
Join Date: Sep 2017
Posts: 1
Alex99 is on a distinguished road
Default Merging multiple CSV files into a new EXCEL workbook


Help needed!!

I have a bunch of individual CSV files and I would need to merge the third column of each CSV file into a new workbook. For example, the third column of the first CSV file goes into the first column of the new EXCEL file, the third column of the second CSV file goes into the second column of the new EXCEL file and so on.

Thanks!
Reply With Quote
  #2  
Old 09-30-2017, 01:54 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is offline Merging multiple CSV files into a new EXCEL workbook Windows 7 64bit Merging multiple CSV files into a new EXCEL workbook Office 2010 64bit
Expert
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,779
Pecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant futurePecoflyer has a brilliant future
Default

Hi and welcome
Have a look at this addin by Ron de Bruin

It's safe, and using the " paste data next to each other " option will do what you need
__________________
Did you know you can thank someone who helped you? Click on the tiny scale in the right upper hand corner of your helper's post
Reply With Quote
  #3  
Old 10-30-2017, 03:34 PM
FionaMcKenzie FionaMcKenzie is offline Merging multiple CSV files into a new EXCEL workbook Windows 10 Merging multiple CSV files into a new EXCEL workbook Office 2016
Novice
 
Join Date: Oct 2017
Location: Surrey, United Kingdom
Posts: 14
FionaMcKenzie is on a distinguished road
Default

Hi there,

Instead of using QueryTable, I recommend using ADO to do this.

I hope this code stub helps you work it out?


Private Function ImportCSVData() As Boolean

On Error GoTo ErrorHandler

Dim oConnection As ADODB.Connection
Dim oRecordset As ADODB.Recordset
Dim strSQL As String, strProvider As String, strADOConnection As String
Dim strFullFileName As String
Dim objSheet As Excel.Worksheet
Dim objStartCell As Excel.Range

ImportCSVData = False

' Turn Excel screenupdating off for speed reasons
Excel.Application.ScreenUpdating = False

' File name to do - in a loop perhaps
strFullFileName = "C:\TestData.csv"

' Your sheet name reference to do
Set objSheet = ActiveSheet

' Your range start to do, i.e. go to end & bring in next data
Set objStartCell = objSheet.Cells(objSheet.Range("A2").CurrentRegion. Rows.Count + 1, 1)

If FSO.FileExists(strFullFileName) Then

' Do a simple SQL query based on your data
strSQL = "Select [Unique Code], [2015], [2016], [2017] FROM " & strFullFileName

Set oConnection = New ADODB.Connection

strADOConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "C:\" & ";" & _
"Extended Properties=""text; HDR=Yes; FMT=Delimited; IMEX=1;"""

oConnection.Open strADOConnection

Set oRecordset = New ADODB.Recordset
oRecordset.ActiveConnection = oConnection
oRecordset.Source = strSQL
oRecordset.Open
objStartCell.CopyFromRecordset oRecordset

oRecordset.Close
oConnection.Close

ImportCSVData = True
End If

CleanUp:
On Error Resume Next

' Always make sure screenupdating is back on
Excel.Application.ScreenUpdating = True
' Clear all object references from memory
Set oConnection = Nothing
Set oRecordset = Nothing
Set objSheet = Nothing
Set objStartCell = Nothing

Exit Function
ErrorHandler:
MsgBox "An unexpected error has occurred" & vbCrLf & Err.Number & " " & Err.Description, _
vbOKOnly + vbInformation, "Import CSV Data"

Err.Clear
GoTo CleanUp
End Function

' This you would typically put into a module called FSO
' You need to reference the Microsoft Scripting Runtime library first
' Select, Tools, References & find the Library in the list and tick it
Public Function FileExists(strFileFullName As String) As Boolean

Dim oFSO As New FileSystemObject

FileExists = oFSO.FileExists(strFileFullName)

Set oFSO = Nothing
End Function
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging multiple CSV files into a new EXCEL workbook Merging Multiple Files by Name, Date abs004 Excel 1 03-02-2017 04:21 PM
Merging multiple CSV files into a new EXCEL workbook Merging multiple fields within a Word letter from Excel LindsayV Mail Merge 2 04-08-2016 07:17 AM
Word does not put the files in the outbox of Outlook (merging with excel fields) Pannemanski Mail Merge 1 04-08-2016 03:58 AM
Merging word files from excel macro hklein Excel Programming 0 08-05-2011 02:27 AM
Merging Multiple Word files into One Stattovic Word 0 01-06-2010 07:19 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft