+ Reply to Thread
Results 1 to 6 of 6

Issues with VBA code to import multiple csv files

  1. #1
    Registered User
    Join Date
    02-24-2018
    Location
    Odense
    MS-Off Ver
    2016
    Posts
    3

    Issues with VBA code to import multiple csv files

    Hi guys. I'm quite new at VBA and seem to have problems at every little step of the way.
    I'm currently trying to make a code that can automatically import a number of csv files into specified sheets in one workbook. I basically just found a youtube video and copied it, but I can't make it work. That is to say that it actually has worked a few times in steps of the process, but when I tried it again it failed for some reason...

    Right now the code is set up to import eight csv files into eight different sheets of the same workbook.
    Filenames of the csv files and the destination sheets are defined in a separat sheet called "forudsætninger" (I'm Danish...)
    The code is built in two loops because the names of the csv files are in two groups with two different prefixes + year and so far I have not found a more elegant way to make this in one loop...

    The code looks as follows:

    Please Login or Register  to view this content.


    I hope some VBA wizard out there can help me

    Best regards from Denmark.
    Last edited by vbabeginner123; 02-24-2018 at 05:18 PM.

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Issues with VBA code to import multiple csv files

    Nice "hole punch" effect, is this code perhaps from perforated cards of Univac ?

  3. #3
    Registered User
    Join Date
    02-24-2018
    Location
    Odense
    MS-Off Ver
    2016
    Posts
    3

    Re: Issues with VBA code to import multiple csv files

    Quote Originally Posted by porucha vevrku View Post
    Nice "hole punch" effect, is this code perhaps from perforated cards of Univac ?
    Yeah we all have to start somewhere

  4. #4
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,151

    Re: Issues with VBA code to import multiple csv files

    Quote Originally Posted by vbabeginner123 View Post
    ... but when I tried it again it failed for some reason ...
    The code is built in two loops because the names of the csv files are in two groups with two different prefixes + year ...
    1. But what's the matter ? What is the reason of this "reason" ?
    2. Which groups ? What are these groups ?
    3. Something more about it.

  5. #5
    Registered User
    Join Date
    02-24-2018
    Location
    Odense
    MS-Off Ver
    2016
    Posts
    3

    Re: Issues with VBA code to import multiple csv files

    Never mind guys, I found the mistake today after trying to describe the script and issue in detail It was a spelling mistake

    But I do have two small questions that I have not been able to work out.

    1. Is there a way to set the for-loop variable (i in my script) to be certain values that you can f.x. write into the Excel-sheet, instead of just jumping in a range with a certain step size? I imagine something like:

    Please Login or Register  to view this content.
    2. Do you see a more elegant way to write the code instead of having two for-loops?
    The reason for the two loops is that I have csv files that are called "name1_year" that should go to sheet "name1*_year" and then csv files called "name2_year" that should go to sheet "name2*_year". Year = 20, 25, 30 and 35.
    But I can't seem to change the names within the same loop...

    Best regards.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: Issues with VBA code to import multiple csv files


    Hi !

    Answer for both questions : just using a For Each loop … (a VBA inner help must read !)

    Example for 1) : For Each V in [{20,35,40}]

    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 07-06-2016, 07:47 PM
  2. [SOLVED] Need Additional Code To Automatically Import New XML Files From Folder
    By BrantBrad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-12-2015, 12:54 PM
  3. [SOLVED] Macro to Import Multiple TXT Files into workbook - User to select files/directory
    By saber007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2013, 08:43 PM
  4. Replies: 9
    Last Post: 02-12-2013, 04:15 PM
  5. Import Multiple txt Files
    By james_ross in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-30-2013, 07:16 AM
  6. [SOLVED] Can't import files, should be simple code
    By Obfuscated in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-09-2012, 02:59 PM
  7. Import of multiple XML files
    By Gzdnkh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-23-2012, 02:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1