Use Macro to Import CSV file to excel

  • Hi all,


    I'm new to Excel VBA and currently working on importing CSV file to excel. Here is my situation:


    I want write a Macro. When people run this Macro, they can choose which CSV to open: the easiest way is a window pops out and let them to select; It can also be like they type the name and location of the CSV file at the beginning, then the Macro can open the CSV file according to the content in the cell. As long as the Macro can open the file according to people's choice, it is good.


    I have tried 2 ways and I will explain them soon:


    First, I tried one example I found online. It has a window popping out and lets me to select the file. The problem is that in the CSV file, there is some content like "5-1", Excel will automatically transfer this to date, which is not I want. Even if I set the cell format as text in advance, it doesn't work at all. The code is as follows:


    Second, I used Record Macro function in excel and got the following code:



    It works fine, but it is strictly to the specific file name and location. I have no idea how to make any change to let me select what I want to import.


    Anyone can give me some advice of how to achieve my goal?


    I'm so sorry that this question is too long. I appreciate in advance for all people that read the question and leave their comments.


    Thanks,
    Robert

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!