Forum Discussion
Importing CSV files with VBA Excel
Hello everyone,
I'm new on this forum. Maybe my questions are already posted there or another forum, but after many research on Internet I haven't found good answers.
My project:
I just want to import 4 CSV files into an existing XLSM workbook, and each into a separate sheet that has been named beforehand.
For example: file1.csv in sheet1, file2.csv in sheet2, file3.csv in sheet3 and file4.csv in sheet4
First question:
I would like to import these files by UserForm and ListBox, with a CommandButton.
Is it possible?
Second question:
If the previous operation is possible, is it possible to display only the name of the imported files on ListBox? (full path displayed by default)
Thanks in advance for your answers
7 Replies
- Juliano-PetrukioBronze Contributor
Hello, is VBA really necessary for this task? I mean, Excel has Power Query feature that you can specify a folder where your CSV files are stored and you can manage all them more easily than by coding.
- SebInfoFRCopper Contributor
Hello Juliano,
Thank you for your reply.
In my first post, I forgot some details. My apologies.
My XLSM file will finally used by my workteam (and me) every month (I work in an IT support). And I have 4 exported files every month from our AD (by PowerShell).
Well, my idea is to use VBA with a UserForm for the import and layout of these files, using CommandButton, for quickly processing of AD data later.
Our main job is to resolve user issues, but we are also responsible for the entry and exit of employees, with different procedures.
I started creating my XLSM file in consultation with my manager, for faster management of employee outings, while respecting our procedures.It is for this purpose that I thought I would use VBA Excel for quick layouts (I think it is possible, but I'm not sure).
Well, I just need to know if it is possible to import csv files into each sheet by their names because I'm blocking on that. I can manage afterwards for the layout.
Do you think it is possible by VBA? Or with Power Query fast enough?
- JMB17Bronze Contributor
Yes, it's possible to import CSV files into your worksheets. Though I'm afraid I don't follow why you want to read the filenames into a listbox first (you could add a command button to a worksheet and just get the filenames and copy the data in)?
I attached a file with a userform and some basic code to read filenames into a listbox. Also, I added a button to import two files. Though these two buttons are, at present, independent of each other (the import button will also have you browse for the files - I don't know what mechanism you plan to use for the import function to open the files since the listbox will only have the file name and not the path).