VBA to import selected data from csv from multiple files to one sheet

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
508
Office Version
  1. 365
Platform
  1. Windows
Hello folks
I have a wb to summarise data from an ever expanding series of csv source files and I've managed to build a routine to copy/paste the required data from those into my summary workbook. The csv's are auto created by a test rig, and it creates a new file for each test. The routine works well, but on reflection there are thousands of these files to be imported, and my code only does one at a time. Is there a way I could select a multiple of source files and process those en mass?
My current code is enclosed and is probably very clunky, but I'm a complete amateur.
How I've configured it is to select the source file via a dialogue window, that opens that file and copy/pastes selected cell data into an intermediate sheet for temporary storage, it then checks for any duplicates of Test Number, and if that's OK, it then finds the next empty row and copy/pastes across into the main sheet.
Is it possible to do this for multiple source files?
Sorry if this is very long winded, but it's the best I can manage I'm afraid.
Thanks folks

PS. I don't know if it's relevent, but it appears that the test rig is adding an .xls extension to the filenames, but when I open them, Excel is seeing them as .csv files. They do seem to be .csv files that the rig is putting the wrong extension on.

Code:
Sub Import_File()
'This code opens the file path to search for the required test data sheet to open via dialog box
'It checks for existing duplicate and then transposes dtat from test sheet to ALL PARTS sheet

'Issue ********************************
'The test data sheets have .xls extension, but are actually only .csv files, this causes the 'Text Import Wizard'
'to action as an additional task.
'**************************************
    
ChDrive "P" ' Changes to drive to search, this must be done before file path can be found below
ChDir "P:\Perkins Tier IV Oil Pump\7 - Facilities & Tools & Gauges\Tier 4 Data Folder\Test" ' Select desired path INCLUDING inserted values
MsgBox "Select required test data sheet from the next window" & vbNewLine & "NOTE! Just click 'Finish' in the 'Text Import Wizard'", , "SELECT SHEET TO IMPORT"
Application.Dialogs(xlDialogOpen).Show ' Open dialog box

'Copy and paste data from sheet to this work book in next available empty row
    ActiveSheet.Range("P1").Value = ActiveWorkbook.FullName
    ActiveSheet.Range("A1:P8").Select
    Selection.Copy
    Application.DisplayAlerts = False
    ActiveWindow.Close
    ActiveWorkbook.Sheets("Calcs").Select
    ActiveSheet.Range("A1").Select
    Application.DisplayAlerts = True
    ActiveSheet.Paste
    
    'Code to search 'Compiled' sheet for current test number and if found already there
    'then cease import with message box
    
    If WorksheetFunction.CountIf(Sheets("Compiled").Range("B:B"), Sheets("Calcs").Range("A12").Value) > 0 Then
    ActiveWorkbook.Sheets("Compiled").Select
    ActiveSheet.Range("B4").Select
    MsgBox "RESULTS FOR THIS TEST NUMBER ALREADY EXIST IN THE LIST", vbExclamation, "SELECT A DIFFERENT SHEET"
    GoTo NoImport
    End If


    'Code to find next empty row and fill with data from Calcs sheet
    ActiveWorkbook.Sheets("Compiled").Activate 'Select starting cell in record sheet, first cell in Test Number range
    Range("B4").Select
    
    Do
    If IsEmpty(ActiveCell) = False Then ' Search for next empty row
        ActiveCell.Offset(1, 0).Select
    End If
    Loop Until IsEmpty(ActiveCell) = True
    
    'Load data from user form to selected empty row
    ActiveCell.Value = ActiveWorkbook.Sheets("Calcs").Range("A12").Value
    ActiveCell.Offset(0, 1) = ActiveWorkbook.Sheets("Calcs").Range("F1").Value
    ActiveCell.Offset(0, 2) = ActiveWorkbook.Sheets("Calcs").Range("B1").Value
    ActiveCell.Offset(0, 3) = ActiveWorkbook.Sheets("Calcs").Range("D5").Value
    ActiveCell.Offset(0, 4) = ActiveWorkbook.Sheets("Calcs").Range("E5").Value
    ActiveCell.Offset(0, 5) = ActiveWorkbook.Sheets("Calcs").Range("D6").Value
    ActiveCell.Offset(0, 6) = ActiveWorkbook.Sheets("Calcs").Range("D7").Value
    ActiveCell.Offset(0, 7) = ActiveWorkbook.Sheets("Calcs").Range("E7").Value
    ActiveCell.Offset(0, 8) = ActiveWorkbook.Sheets("Calcs").Range("D8").Value
    ActiveCell.Offset(0, 9) = ActiveWorkbook.Sheets("Calcs").Range("E8").Value
    

NoImport:

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Thanks VoG, that might move me forward. I've got that working even though the csv files are 'disguised' as xls files and that has imported cleanly the group I selected into individual sheets in my wb. I don't want to keep all of those though, so i just need to work out how to summarise the selected cells from each new sheet into my summary sheet and then delete all those new tabs again.


I'm taking a deep breath and going for it.

Thanks again, I'll probably be back though :o(
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top