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

Поделиться
HTML-код
  • Опубликовано: 15 окт 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 RUclips
    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

Комментарии • 163

  • @MelCompton
    @MelCompton  Год назад +35

    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.

  • @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.

  • @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!

  • @joshuagoodman1763
    @joshuagoodman1763 9 месяцев назад +1

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

  • @DK_Son
    @DK_Son 2 месяца назад +1

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

    • @MelCompton
      @MelCompton  Месяц назад +1

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

  • @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.

  • @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?

  • @phylum8975
    @phylum8975 6 месяцев назад

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

  • @3756hans
    @3756hans Год назад

    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.

  • @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!

  • @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.

  • @derekulrich3830
    @derekulrich3830 Год назад +1

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

    • @MelCompton
      @MelCompton  Год назад

      Thank you! I'm glad it helped!

  • @donnasaud5108
    @donnasaud5108 2 месяца назад +1

    YOU'RE A LIFE SAVER! THANK YOU!!

    • @MelCompton
      @MelCompton  Месяц назад

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

  • @budhall4296
    @budhall4296 10 месяцев назад +1

    Exactly what I needed. Thank you!

  • @appleseed_316
    @appleseed_316 3 месяца назад +1

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

    • @MelCompton
      @MelCompton  Месяц назад

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

  • @jakuburban6289
    @jakuburban6289 Год назад +1

    You just saved me hours worth of work!

  • @KooAdams
    @KooAdams 8 месяцев назад

    Thanks @MelCompton this was exactly what i needed!

  • @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!

  • @Personal-pf8iy
    @Personal-pf8iy 8 месяцев назад +1

    I sooooo neeed this. Thank you!

  • @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!

  • @ErikForeman-fe8ub
    @ErikForeman-fe8ub 3 месяца назад +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  Месяц назад

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

  • @JamesBond-qo8th
    @JamesBond-qo8th 4 месяца назад +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  Месяц назад

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

  • @RamblingCutie
    @RamblingCutie 5 месяцев назад

    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!

  • @bikrampandher
    @bikrampandher Год назад +1

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

  • @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!

  • @MCreims
    @MCreims Год назад +1

    you rock, thank you Melissa!

  • @BlackgoldTwinduck
    @BlackgoldTwinduck 11 месяцев назад

    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?

  • @salmanmusliman5153
    @salmanmusliman5153 11 месяцев назад

    this is extremely helpful! thanks for your efforts!!

  • @mcmbuckley
    @mcmbuckley Год назад

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

  • @stevesosa5844
    @stevesosa5844 5 дней назад

    Instead of CSV can i change the sub line to .xls workbooks?

  • @alloy299
    @alloy299 8 месяцев назад

    Thank you very much, this was really helpful.

  • @carolinaiga-musisi4342
    @carolinaiga-musisi4342 5 месяцев назад

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

    • @MelCompton
      @MelCompton  5 месяцев назад

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

  • @favoursamuel8963
    @favoursamuel8963 2 месяца назад +1

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

    • @MelCompton
      @MelCompton  Месяц назад

      Unfortunately this is not possible in Google Sheets.

  • @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

  • @courtneypierone5499
    @courtneypierone5499 Год назад +1

    Thank you! this will save me hours of work!

  • @grantschrof7740
    @grantschrof7740 Год назад +1

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

    • @MelCompton
      @MelCompton  Год назад

      Thank you! Glad it was helpful!

  • @adejyde4899
    @adejyde4899 Год назад

    this video is a blessing...Thank you

  • @henryp218
    @henryp218 7 месяцев назад

    Thank you so much for this! This is amazing!

  • @martaduque210
    @martaduque210 8 месяцев назад +1

    Thank you! Thank you so much!

  • @tjforce24
    @tjforce24 8 месяцев назад

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

  • @mattbown
    @mattbown Год назад

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

  • @yy8325
    @yy8325 10 месяцев назад +1

    thank you for this!

  • @RexDavid
    @RexDavid Год назад +1

    awesome! this saved so much time! Thanks!

    • @MelCompton
      @MelCompton  Год назад

      Thank you! I'm glad it helped!

  • @nirmalnirmal3720
    @nirmalnirmal3720 9 месяцев назад

    Thank you so much. Saved hours of work 🙏

    • @MelCompton
      @MelCompton  9 месяцев назад

      Glad it helped!

    • @nirmalnirmal3720
      @nirmalnirmal3720 8 месяцев назад

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

  • @ulrich4060
    @ulrich4060 Год назад +2

    This was so great! Thank you so much!

    • @MelCompton
      @MelCompton  Год назад +1

      Thank you! I'm happy it helped!

  • @themysterious7505
    @themysterious7505 Год назад +1

    Thank you melissa

    • @MelCompton
      @MelCompton  Год назад

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

  • @ArikWitono-y4m
    @ArikWitono-y4m 8 месяцев назад

    thanks for your share, your macro is helpful

  • @archanasahoo9670
    @archanasahoo9670 Год назад

    THANK YOU SO MUCH
    ☺IT HELPED ME A LOT

  • @mikegold9455
    @mikegold9455 11 месяцев назад

    So helpful, thanks!

  • @EduardoFlores-uw7fs
    @EduardoFlores-uw7fs 4 месяца назад

    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?

  • @kevinpugh1390
    @kevinpugh1390 Год назад

    That was super helpful - thank you

  • @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

  • @nishantgautam4343
    @nishantgautam4343 Год назад

    It was very useful, thank you.

  • @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.

  • @ashishmishra0475
    @ashishmishra0475 4 месяца назад

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

  • @VinayKumar-iu9ku
    @VinayKumar-iu9ku Год назад +1

    thank you very much 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.

  • @TheWhitevitz
    @TheWhitevitz Год назад +2

    You are a life saviour!!!

  • @KimSamouelle
    @KimSamouelle 5 месяцев назад

    Perfect, thank you.

  • @AaravAggarwal-b8i
    @AaravAggarwal-b8i Год назад

    I can not thank you enough. Awesome content!

  • @EshikaShah-w1d
    @EshikaShah-w1d Год назад

    Thanks melissa.

  • @Sabuz73
    @Sabuz73 3 месяца назад

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

    • @MelCompton
      @MelCompton  2 месяца назад +1

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

    • @BeentoTheCross
      @BeentoTheCross 2 месяца назад

      @@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?

  • @eduardoguevara8014
    @eduardoguevara8014 7 месяцев назад

    Very useful. Thanks

  • @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.

  • @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.

  • @pointofreview4348
    @pointofreview4348 Год назад

    Thank you so much 😍

  • @stefapaco
    @stefapaco 6 месяцев назад

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

  • @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)?

  • @ancienttradition_stx
    @ancienttradition_stx 7 месяцев назад

    THANK YOU!

  • @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.

  • @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.

  • @leotran3184
    @leotran3184 Год назад +1

    Thank you very much!

    • @MelCompton
      @MelCompton  Год назад

      Thank you! I'm glad it helped!

  • @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!

  • @dangoldstein7984
    @dangoldstein7984 Месяц назад +1

    Thanxxxxxx ALOT !

    • @MelCompton
      @MelCompton  Месяц назад

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

  • @mariasouladaki9438
    @mariasouladaki9438 6 месяцев назад

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

  • @nirmalmathew4312
    @nirmalmathew4312 Месяц назад

    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.

  • @mummypoko1530
    @mummypoko1530 3 месяца назад +1

    Thank you

    • @MelCompton
      @MelCompton  Месяц назад

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

  • @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.

  • @AK-bo7yq
    @AK-bo7yq 10 месяцев назад

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

  • @srikrishnasagar2909
    @srikrishnasagar2909 9 месяцев назад

    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 9 месяцев назад

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

    • @MelCompton
      @MelCompton  9 месяцев назад

      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 9 месяцев назад

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

  • @bluevalley82
    @bluevalley82 7 месяцев назад

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

  • @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!

  • @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.

  • @duralesin
    @duralesin 9 месяцев назад

    I love you. It is awesome

  • @LoizaMartin
    @LoizaMartin Год назад

    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.

  • @mahdikazemi5306
    @mahdikazemi5306 5 месяцев назад

    Great 👌👌👌👌👌👌

  • @Md.AbdullahAlMahin-u7p
    @Md.AbdullahAlMahin-u7p Год назад

    Perfect😀

  • @uma1910
    @uma1910 6 месяцев назад

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

    • @uma1910
      @uma1910 6 месяцев назад

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

  • @mahdikazemi5306
    @mahdikazemi5306 5 месяцев назад

    👌👌👌👌👌👌👌👌👌👌

  • @bastardboyyeah3731
    @bastardboyyeah3731 Год назад +1

    Absolute lifesaver, thank you for code.

  • @shahrukhal1
    @shahrukhal1 Год назад

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