How to Import Multiple CSV Files into Separate Worksheets in the Same Workbook in Excel

Поділитися
Вставка
  • Опубліковано 12 вер 2024
  • In this tutorial you will learn how to import multiple CSV files into separate worksheets in the same workbook in Excel. Instead of combining the CSV files into one worksheet, you can use a Visual Basic Script in Excel to import multiple CSV files into different tabs. The VB Script will name each Microsoft Excel tab with the same name as the CSV file. Once you have imported the CSV files into the worksheet they can be saved and formatted like any other Excel workbook.
    Visual Basic code: It would not let me put it in the description so I put it as a pinned comment.
    Support me with your amazon purchases: melcompton.com....
    Check out these programs I use for UA-cam
    Bluehost discount: melcompton.com....
    Canva Pro Trial: melcompton.com....
    Adobe: melcompton.com...
    Filmora: melcompton.com....
    Follow me on social media:
    Facebook: / therealmelco. .
    Twitter: @melissaecompton
    Instagram: @melissaacompton
    LinkedIn: / melissa-c. .
    This description contains affiliate links and I may be paid a small commission should you purchase using these links.
    #melissacompton #msexcel #excel #importcsvfiles

КОМЕНТАРІ • 162

  • @MelCompton
    @MelCompton  Рік тому +34

    VISUAL BASIC CODE:
    Sub CombineCsvFiles()
    'updated by MelCompton
    Dim xFilesToOpen As Variant
    Dim I As Integer
    Dim xWb As Workbook
    Dim xTempWb As Workbook
    Dim xDelimiter As String
    Dim xScreen As Boolean
    On Error GoTo ErrHandler
    xScreen = Application.ScreenUpdating
    Application.ScreenUpdating = False
    xDelimiter = "|"
    xFilesToOpen = Application.GetOpenFilename("Text Files (*.csv), *.csv", , "MelCompton VBA for Excel", , True)
    If TypeName(xFilesToOpen) = "Boolean" Then
    MsgBox "No files were selected", , "MelCompton VBA for Excel"
    GoTo ExitHandler
    End If
    I = 1
    Set xTempWb = Workbooks.Open(xFilesToOpen(I))
    xTempWb.Sheets(1).Copy
    Set xWb = Application.ActiveWorkbook
    xTempWb.Close False
    Do While I < UBound(xFilesToOpen)
    I = I + 1
    Set xTempWb = Workbooks.Open(xFilesToOpen(I))
    xTempWb.Sheets(1).Move , xWb.Sheets(xWb.Sheets.Count)
    Loop
    ExitHandler:
    Application.ScreenUpdating = xScreen
    Set xWb = Nothing
    Set xTempWb = Nothing
    Exit Sub
    ErrHandler:
    MsgBox Err.Description, , "MelCompton VBA for Excel"
    Resume ExitHandler
    End Sub

    • @RahulYadav-qn7jv
      @RahulYadav-qn7jv Рік тому +1

      Hi Melissa,
      thanks for the code.
      But it imports the csf files in a new excel file.
      i want to import the csv in the same workbook where i am saving the vba code.
      can you please help

    • @MelCompton
      @MelCompton  Рік тому +2

      @@RahulYadav-qn7jv Hi! I would not recommend doing it in the same workbook. It is easy to accidently change the VB code and then it not work or do something crazy. I recommend saving the VB code in a separate workbook and naming it template to keep this from happening.

    • @RahulYadav-qn7jv
      @RahulYadav-qn7jv Рік тому

      @@MelCompton Actually i am using your code as a part of big macro. I want to import all CSV file in a specific Excel sheet.

    • @TheWhitevitz
      @TheWhitevitz Рік тому

      HI Melissa, First thanks for the code is really helpful.
      Will be possible to import to the same excel sheet where I execute the Macro? I want to use your macro as one of the steps in larger data preparation and the goal is to have one template that can trigger the base of the action in steps trigger by buttons.... if you can support me on this will be awesome! thanks.,

    • @fredflintstone505
      @fredflintstone505 Рік тому

      Is it possible to pull the csv data in with a specific Table Format? Like it does when you import a csv manually.

  • @brianwallace3584
    @brianwallace3584 День тому +1

    Amazing. This just saved me a thousand dollars of time. So grateful! Thank you!!

    • @MelCompton
      @MelCompton  День тому

      Thank you so much! I am glad it helped!

  • @jdzfb
    @jdzfb Рік тому +7

    This was perfect, exactly what I needed! Your 5 minute video saved me hours of manual work

    • @Serenity_Smiles
      @Serenity_Smiles Рік тому

      where can I find the code?

    • @MelCompton
      @MelCompton  Рік тому

      Hello! It wouldn’t let me put it in the description so it’s pinned in a comment.

  • @DK_Son
    @DK_Son Місяць тому +1

    This was incredible, and so easy to follow along with! Thank you for this!

    • @MelCompton
      @MelCompton  8 днів тому +1

      You are very welcome! I am glad this helped you!

  • @phylum8975
    @phylum8975 5 місяців тому

    Well cone Melissa! You made this super easy to follow and helped me import 16 CSV's in less than 5 minutes!

  • @joshuagoodman1763
    @joshuagoodman1763 7 місяців тому +1

    THANK YOU SO MUCH this worked perfect I have been trying to figure out how to do this for awhile..

  • @krishnasahoo4598
    @krishnasahoo4598 Рік тому

    This was the exact thing I needed and I really searched so many yt videos but ..Finally ..I got this...Thanks Melissa for making this.

  • @3756hans
    @3756hans 11 місяців тому

    Man that was a Great. First time somebody went through all the steps .I was able to do it first try on split screen. I even made a small button on the template ( new for me too ) to run the macro.

  • @BS-ni8ww
    @BS-ni8ww Рік тому

    very nice, my daughter had the need for this for college and I helped her find it.

  • @appleseed_316
    @appleseed_316 2 місяці тому +1

    Thanks this saved me pulling out my remaining few hairs !
    🥰

    • @MelCompton
      @MelCompton  8 днів тому

      You are very welcome! I'm happy it helped you!

  • @Fr_dG
    @Fr_dG Рік тому +1

    Well done! I was trying to figure out a good way to combine data for the data analytics certification, and this helped me so much. Thank you!

    • @MelCompton
      @MelCompton  Рік тому +1

      Thank you! I'm glad it helped!

  • @jakuburban6289
    @jakuburban6289 Рік тому +1

    You just saved me hours worth of work!

  • @donnasaud5108
    @donnasaud5108 Місяць тому +1

    YOU'RE A LIFE SAVER! THANK YOU!!

    • @MelCompton
      @MelCompton  8 днів тому

      You are very welcome! I am glad this helped you!

  • @budhall4296
    @budhall4296 8 місяців тому +1

    Exactly what I needed. Thank you!

  • @derekulrich3830
    @derekulrich3830 Рік тому +1

    boom, 382 CSV files imported into different files. thanks, this worked great

    • @MelCompton
      @MelCompton  Рік тому

      Thank you! I'm glad it helped!

  • @mcmbuckley
    @mcmbuckley Рік тому

    Awesome video. So useful and made my job 1k times easier. Nice job

  • @aodhang9120
    @aodhang9120 Рік тому +1

    Thank you so very much Melissa. What an amazingly clear and consise video

    • @MelCompton
      @MelCompton  Рік тому

      Thank you! I'm happy it helped!

  • @MCreims
    @MCreims Рік тому +1

    you rock, thank you Melissa!

  • @Personal-pf8iy
    @Personal-pf8iy 7 місяців тому +1

    I sooooo neeed this. Thank you!

  • @JJ-cc2eh
    @JJ-cc2eh Рік тому +5

    Thank you for making this available. I am trying to get it to run for Office for Mac. I can save the macro but when I try to run it it throws an error: "Methos 'GetOpenFilename' of object '_Application' failed". Any ideas?

  • @bikrampandher
    @bikrampandher Рік тому +1

    Thank you so much for such an amazing tutorial and providing code as well.

  • @user-gr8uz5ri4n
    @user-gr8uz5ri4n 7 місяців тому

    Thanks @MelCompton this was exactly what i needed!

  • @carolinaiga-musisi4342
    @carolinaiga-musisi4342 4 місяці тому

    I love this macro so much! It is a life saver!👏

    • @MelCompton
      @MelCompton  4 місяці тому

      I am so glad it helped and thank you for the super!!!

  • @grantschrof7740
    @grantschrof7740 Рік тому +1

    Melissa! Thank you, this is a huge help!!

    • @MelCompton
      @MelCompton  Рік тому

      Thank you! Glad it was helpful!

  • @courtneypierone5499
    @courtneypierone5499 Рік тому +1

    Thank you! this will save me hours of work!

  • @ulrich4060
    @ulrich4060 Рік тому +2

    This was so great! Thank you so much!

    • @MelCompton
      @MelCompton  Рік тому +1

      Thank you! I'm happy it helped!

  • @martaduque210
    @martaduque210 6 місяців тому +1

    Thank you! Thank you so much!

  • @salmanmusliman5153
    @salmanmusliman5153 10 місяців тому

    this is extremely helpful! thanks for your efforts!!

  • @RexDavid
    @RexDavid Рік тому +1

    awesome! this saved so much time! Thanks!

    • @MelCompton
      @MelCompton  Рік тому

      Thank you! I'm glad it helped!

  • @alloy299
    @alloy299 7 місяців тому

    Thank you very much, this was really helpful.

  • @dangoldstein7984
    @dangoldstein7984 17 днів тому +1

    Thanxxxxxx ALOT !

    • @MelCompton
      @MelCompton  8 днів тому

      You are very welcome! I am glad this helped you!

  • @yy8325
    @yy8325 9 місяців тому +1

    thank you for this!

  • @bastardboyyeah3731
    @bastardboyyeah3731 Рік тому +1

    Absolute lifesaver, thank you for code.

  • @henryp218
    @henryp218 6 місяців тому

    Thank you so much for this! This is amazing!

  • @mattbown
    @mattbown Рік тому

    This is amazing.... Thank you so much for posting this.

  • @nirmalnirmal3720
    @nirmalnirmal3720 8 місяців тому

    Thank you so much. Saved hours of work 🙏

    • @MelCompton
      @MelCompton  7 місяців тому

      Glad it helped!

    • @nirmalnirmal3720
      @nirmalnirmal3720 7 місяців тому

      @@MelCompton It doesn't work if files have multiple sheets in them. Does it??

  • @VinayKumar-iu9ku
    @VinayKumar-iu9ku Рік тому +1

    thank you very much Melissa

  • @adejyde4899
    @adejyde4899 Рік тому

    this video is a blessing...Thank you

  • @mummypoko1530
    @mummypoko1530 2 місяці тому +1

    Thank you

    • @MelCompton
      @MelCompton  8 днів тому

      You are very welcome! I'm happy it helped you!

  • @themysterious7505
    @themysterious7505 Рік тому +1

    Thank you melissa

    • @MelCompton
      @MelCompton  Рік тому

      Hello! Thank you! I'm happy it helped!

  • @ArikWitono-y4m
    @ArikWitono-y4m 7 місяців тому

    thanks for your share, your macro is helpful

  • @archanasahoo9670
    @archanasahoo9670 Рік тому

    THANK YOU SO MUCH
    ☺IT HELPED ME A LOT

  • @nishantgautam4343
    @nishantgautam4343 Рік тому

    It was very useful, thank you.

  • @TheWhitevitz
    @TheWhitevitz Рік тому +2

    You are a life saviour!!!

  • @kevinpugh1390
    @kevinpugh1390 Рік тому

    That was super helpful - thank you

  • @eduardoguevara8014
    @eduardoguevara8014 6 місяців тому

    Very useful. Thanks

  • @mikegold9455
    @mikegold9455 10 місяців тому

    So helpful, thanks!

  • @user-km3qk8lw9e
    @user-km3qk8lw9e Рік тому

    I can not thank you enough. Awesome content!

  • @KimSamouelle
    @KimSamouelle 4 місяці тому

    Perfect, thank you.

  • @michelhagendoorn
    @michelhagendoorn Рік тому

    Thanks Melissa what a good piece of basic programming
    Is it also possible that a specific folder is opened and that I can browse from there and then choose the csv files.
    Regards,
    Michel.

  • @RamblingCutie
    @RamblingCutie 4 місяці тому

    This is the closest thing I've found to what I am looking for! However instead of opening into a new sheet I'd like subsequent CSVs to open in the adjacent columns. Each CSV has two columns of data so for example the first CSV imports into columns A & B, the second csv imports into columns C & D, the third into E &F and so on. Is there a way to modify this code to have it import my data like that? Thank you for your help!

  • @Forgirlsonly12
    @Forgirlsonly12 Рік тому +2

    It doesn't work for me unfortunately, I can not select my files when running the macro even though I have txt. files. Could you help me, please

  • @leotran3184
    @leotran3184 Рік тому +1

    Thank you very much!

    • @MelCompton
      @MelCompton  Рік тому

      Thank you! I'm glad it helped!

  • @mahdikazemi5306
    @mahdikazemi5306 4 місяці тому

    Great 👌👌👌👌👌👌

  • @syedzeeshanhussain5858
    @syedzeeshanhussain5858 Рік тому

    You are awesome...love you...thanks for the code...

    • @Serenity_Smiles
      @Serenity_Smiles Рік тому

      where can I find the code?

    • @MelCompton
      @MelCompton  Рік тому

      Hello! It wouldn’t let me put it in the description so it’s pinned in a comment.

  • @favoursamuel8963
    @favoursamuel8963 Місяць тому +1

    Thanks for this, how do I achieve same result in Google Sheets?

    • @MelCompton
      @MelCompton  8 днів тому

      Unfortunately this is not possible in Google Sheets.

  • @ancienttradition_stx
    @ancienttradition_stx 6 місяців тому

    THANK YOU!

  • @ErikForeman-fe8ub
    @ErikForeman-fe8ub 2 місяці тому +1

    Thank you for the code. The delimiter does not seem to be used in the code. I need to set the delimiter to something specific. Can you help please?

    • @MelCompton
      @MelCompton  2 години тому

      Hi! CSV is Comma Separated Values. What is your delimiter?

  • @pointofreview4348
    @pointofreview4348 Рік тому

    Thank you so much 😍

  • @michaelsee4013
    @michaelsee4013 Рік тому

    Thanks Melissa, works perfectly to what I need except is there a way to delimite using semicolons or comma? I am a noob in VBA, so appreciate your help!

  • @user-nr7oj3mi8f
    @user-nr7oj3mi8f Рік тому

    Thanks melissa.

  • @Snpcam
    @Snpcam Рік тому +4

    Thank you. I’m trying to run it on mac but am getting an error message “method’getopenfilename’ of object’_Application’failed

    • @joellocherty298
      @joellocherty298 Рік тому

      Same

    • @stipepavicic677
      @stipepavicic677 Рік тому

      I've experienced same error....

    • @MelCompton
      @MelCompton  Рік тому

      Hello! This is not compatible on a MAC. Unfortunately Microsoft doesn't fully code for MAC's so some functionality is missing. Hopefully they will change that in the future.

    • @MelCompton
      @MelCompton  Рік тому

      Hello! This is not compatible on a MAC. Unfortunately Microsoft doesn't fully code for MAC's so some functionality is missing. Hopefully they will change that in the future.

    • @MelCompton
      @MelCompton  Рік тому

      Hello! This is not compatible on a MAC. Unfortunately Microsoft doesn't fully code for MAC's so some functionality is missing. Hopefully they will change that in the future.

  • @JamesBond-qo8th
    @JamesBond-qo8th 2 місяці тому +1

    Is there a visual basic code to help power query load Csv files from a folder location automatically as opposed to having the user individually load csv files from power query?

    • @MelCompton
      @MelCompton  2 години тому

      Hi! This is a great suggestion! I will add it to my list to research and make a tutorial.

  • @tjforce24
    @tjforce24 7 місяців тому

    This works great. Confused by the variable: xDelimiter = "|". I have pipe (|) delimited text files and wondering how you change the file delimiter?

  • @bluevalley82
    @bluevalley82 6 місяців тому

    How can we copy the code down? Thank you so much!

  • @phdnofuddy
    @phdnofuddy Рік тому

    Great tutorial! Thank you.
    I have a slightly different need. I need to import data from multiple csv files onto ONE worksheet. In other words, make one sheet where the information from each new file is appended to the information imported from the previous file.
    How would I adjust this code to accomplish that?
    Thanks for considering!

  • @BlackgoldTwinduck
    @BlackgoldTwinduck 10 місяців тому

    This looks great!
    Althoughi have one issue, we use semi colon separated csv. Any tipps how I could adjust the macro to work with those?

  • @duralesin
    @duralesin 8 місяців тому

    I love you. It is awesome

  • @ashishmishra0475
    @ashishmishra0475 3 місяці тому

    How can I make this VBA code to be applied in all the excel file whenever I open a new excel file

  • @EduardoFlores-uw7fs
    @EduardoFlores-uw7fs 3 місяці тому

    Now if I did this and changed 150 csv files into 1 workbook, how do I give them format to not have all the information in just 1 column?

  • @stefapaco
    @stefapaco 5 місяців тому

    did the steps for me its just creating a new sheet, any idea why?

  • @mariasouladaki9438
    @mariasouladaki9438 5 місяців тому

    How can we do the same function but with .xls files?

  • @Sabuz73
    @Sabuz73 Місяць тому

    Very informative? Can i do same import from text files as well ?

    • @MelCompton
      @MelCompton  Місяць тому +1

      Hi. Yes it should work the same way. :)

    • @BeentoTheCross
      @BeentoTheCross 28 днів тому

      @@MelCompton When I navigate to the folder with my text files, there are no files to select. Any ideas why I am not seeing my files?

  • @shaikmoulali7990
    @shaikmoulali7990 Рік тому

    Hello Melissa,
    Thankyou for the video, it is well informative.
    But in my case, instead of . csv files, i have . out files.
    How to deal with it. Can you please help me here.

  • @leemillen4374
    @leemillen4374 Рік тому +1

    Thank you! This is great, though my CSV files' data with leading zeros are losing their leading zeros. For example, Zip Codes with leading zeros are importing, but leading zeros are gone once in Excel. Is there a way to make all CSV columns being imported with the macro to be text? Again, my thanks!

    • @MelCompton
      @MelCompton  Рік тому +2

      Yes. It will take some extra VBA code. I will get it written and tutorial put out as soon as I can.

  • @GunjanAmazon
    @GunjanAmazon Рік тому

    Hi @melissa, Firstly Thank you for the code and guide can you share the changes for Mac as this doesnt work in Mac

    • @MelCompton
      @MelCompton  Рік тому

      Hello! Unfortunately this is not compatible with MAC.

  • @sabhapathikrupa3259
    @sabhapathikrupa3259 Рік тому +1

    Hi Melisa thank you... I am not able to find the pinned comment could you please help

    • @MelCompton
      @MelCompton  Рік тому

      Hello! I re-pinned the comment. You should be able to see it now.

  • @user-qy3gt1ed1p
    @user-qy3gt1ed1p Рік тому

    Perfect😀

  • @AK-bo7yq
    @AK-bo7yq 9 місяців тому

    I am continuously getting a type mismatch error.. Can you help ?

  • @sharonmccormick6987
    @sharonmccormick6987 Рік тому +1

    Hi Melissa, how would the code change if I wanted to add the tabs to an existing workbook

    • @MelCompton
      @MelCompton  Рік тому

      Hello! I will pop out a quick short and explain how to do this as soon as I can.

  • @shwetalimje4004
    @shwetalimje4004 Рік тому

    Hey , I tried this code but there is an error showing up that says " That name is already taken" in a melcompton vba dailogue box. Is there any solution to this. Kindle let me know

  • @anantwaje6539
    @anantwaje6539 Рік тому +1

    Hi..thank you but can we add 2 csv files in single worksheet. Pls help

    • @MelCompton
      @MelCompton  Рік тому

      Hello! Yes you can. I am putting together a quick tutorial on how to do this!

  • @anyab.9533
    @anyab.9533 Рік тому

    Very helpful! However, I need to import multiple Excel files into corresponding sheets in my existing report template. How can I do that?

    • @MelCompton
      @MelCompton  Рік тому

      Hello! Are you wanting to import multiple CSV's or actual Excel files (.xlxs)?

  • @srikrishnasagar2909
    @srikrishnasagar2909 8 місяців тому

    Thanks for the video. Im trying to do the same in macbook but im getting an error "Method 'GetOpenFilename' of object '_Application' failed". Could you please help me with it? Advance Thanks.

    • @dewisutopo
      @dewisutopo 7 місяців тому

      yeah, same with you. I just try in MacBook get error like this

    • @MelCompton
      @MelCompton  7 місяців тому

      VB on a MAC is not supported by Microsoft. The version of MS Office for the MAC is limited and unfortunately one of the key functions that is not compatible is VB. Hopefully one day Microsoft and Apple (the silicon behind the MAC) will play nice, but we will have to see. :)

    • @dewisutopo
      @dewisutopo 7 місяців тому

      @@MelCompton thank you for responding, do you have any other method?

  • @user-nv6js5kx3r
    @user-nv6js5kx3r Рік тому

    I get the error: “That name is already taken. Try a different one.”
    How do I fix this?

  • @Serenity_Smiles
    @Serenity_Smiles Рік тому +1

    HI Melissa, where can I find the code?

    • @MelCompton
      @MelCompton  Рік тому +1

      Hello! It wouldn’t let me put it in the description so it’s pinned in a comment.

  • @hlo512
    @hlo512 Рік тому

    Hello Mellisa, where do i go to get the vba code? Thank you.

    • @MelCompton
      @MelCompton  Рік тому

      Hello! It wouldn’t let me put it in the description so it’s pinned in a comment.

  • @nirmalmathew4312
    @nirmalmathew4312 25 днів тому

    Thank You for this Tutorial. However, I do have few .txt files that have leading zero's in one of the cell. Those are getting trimmed in excel. So in the VB script is there any code we can add so that when macro imports the data from .txt to excel in text format (rather than general) so that leading zero's are preserved?

    • @MelCompton
      @MelCompton  День тому

      If the .csv files are saved with the columns formatted with the leading zero's it should not be stripping them. You can also try using the text functiion to preserve the zero's before saving the .csv. An example would be =TEXT(A1,"00000) then copy and paste special. I will look and see if there is something I can add to the code to keep this from happening on import. But, if there are gone at the time of saving the .csv file then the code won't see them.

  • @ramutsindelashumani1076
    @ramutsindelashumani1076 Рік тому

    Hi Melissa,
    It is throwing an error : Object variable or With block variable not set
    after having loaded a few files into the Excel workbook.
    Please help

    • @ramutsindelashumani1076
      @ramutsindelashumani1076 Рік тому +1

      The error was thrown because i had opened one of the files.
      Thanks again, Melissa for your beautiful work.

    • @MelCompton
      @MelCompton  Рік тому

      Thank you! I'm glad it helped!

  • @mahdikazemi5306
    @mahdikazemi5306 4 місяці тому

    👌👌👌👌👌👌👌👌👌👌

  • @uma1910
    @uma1910 5 місяців тому

    Is it possible to Import Multiple excel Files into Separate Worksheets in the Same Workbook in Excel?

    • @uma1910
      @uma1910 5 місяців тому

      By replacing, csv files to the xlsx it is easily achievable. Thanks.

  • @shahrukhal1
    @shahrukhal1 Рік тому

    Thank you melissa
    give me your linkdln pls the above link isn't working