Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Add the CSV filename to a cell after import

0

Hello,

I would like to know if there is a way after anybody imports a CSV file into excel into a new sheet in cell A2. I would like to know if there is a formula or macro, that you can input in cell A1, the CSV filename of the data that got imported? Thank you for any help I can get.

Answer
Discuss

Discussion

Hi there, that is very easy to do, compared to the import, but how you do it depends a lot on how you get the name of the file that you import. Once you have the name though, its just a matter of Worksheets("Sheet name").Range("A1").Value = "File name" If you post the code you already have, or the part of it that gets the file name, it will be easier to show you what to put for the "File name" part since that will probably be a variable and not hard-coded text.
don (rep: 1989) May 3, '21 at 4:23 pm
At the time of opening the file for import its name is definitely known. After import is complete, however, it's forgotten. Therefore, anything to do with the file name must be dealt with before or during the import, including taking a note of it for later use. Therefore you should disclose your import procedure. Edit your question to include it there. If you use code, please publish the code and we'll show you where to extract the file name. But if you don't use code for the import, are you aware that you could just open a CSV file in Excel just as you would open an Excel workbook?
Variatus (rep: 4889) May 3, '21 at 8:13 pm
Add to Discussion

Answers

0

Hi Jagra and welcome to the Forum

When you import the CSV file into an unsaved file (or open one) , put this in a cell, say A1, BEFORE you save it in Excel format:

=CELL("filename")
then fix it e.g. copy/paste special (text only). Works for Excel 2007 and all later versions. 

That will give you the current filename with its full path. Use text manipulation if you want just the fliename. See the Tutorials section for guidance on RIGHT, LEN etc.

Discuss

Discussion

Jagra

This is the second time that you have not responded to an Answer from me. Kindly check the rules of the Forum before you ask any future questions.
John_Ru (rep: 6142) May 6, '21 at 12:39 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login