Importing CSV files with VBA Excel

Copper Contributor

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

@SebInfoFR 

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.

 

Juliano-Petrukio_0-1598806075628.png

 

@Juliano-Petrukio 

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?

@SebInfoFR 

 

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).

 

 

 

 

@JMB17 

Hello JMB17,

 

Thanks for your reply, and thanks for your file. I will visualize it as soon as I have time (working this week, I might have a lot of work).
I keep you informed of the continuation.

For my 2nd question, it was just to show only the file names in ListBox, to make it more intuitive for my workteam. But if that is not possible, it's not important.

@JMB17 

 

Hello JMB17,

 

I hope you are well.

 

I have checked and tested your VBA code in my XLSM file, and it works perfectly.

Eventually, I have deleted ListBox and others CommandButtons on my original UserForm.

 

Really, thank you very much, you helped me a lot on the part where I was blocking for a long time.

 

Have a nice day.

You're welcome, I'm glad to hear you are able to move forward on your project.

@JMB17 

 

Yes, and you help me on another point: my first MultiSelect:=True (on Application.GetOpenFileName) of my code didn't work because I had declared the variable "fNames" (taking the example of your XLSM) As String (I had only learned this option).
I hadn't thought to explore other options availabled, including As Variant.

 

I'm still quite a beginner, so I have a lot to learn about VBA, and Excel in general.

I think this software is really powerful and efficient.