+ Reply to Thread
Results 1 to 21 of 21

Importing data from CSV/TEXT file

  1. #1
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Importing data from CSV/TEXT file

    Hello members,

    I am trying to set up a vba code with no success. Is it possible to import CSV file with macro ? I want to go on date tab > choose From Text/CSV > Import the file and click Load automatically.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    329

    Re: Importing data from CSV/TEXT file

    This is a very simple loop in a button

    Please Login or Register  to view this content.
    edit: noticed incorrect do while loop
    Attached Files Attached Files
    Last edited by jdelano; 10-11-2024 at 07:47 AM.

  3. #3
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Thank you for providing the vba code. It doesnt work for some reason. When I add this vba code and try to run it, I am getting error on "Dim textFile As Textstrem" - Compile error:User-defined type not defined.

  4. #4
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    329

    Re: Importing data from CSV/TEXT file

    this message here ' must reference Microsoft Scripting RunTime - you need to add this reference to your project in the VBA Editor

  5. #5
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    I am sorry, but where exactly to add it ? The steps I did, Take the vba code and place it my module, than change the Sub to my name and after I run it, I got this message above.

  6. #6
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    329

    Re: Importing data from CSV/TEXT file

    Tools > Reference > scroll down to Microsoft Scripting Runtime > click the checkbox > click ok

    https://bettersolutions.com/vba/visu...references.htm

  7. #7
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Thank you very much. Its working now, but my file is in CSV format... sorry I forgot to send the csv, is it possible to add csv ?

  8. #8
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    329

    Re: Importing data from CSV/TEXT file

    You can simply change the delimiter on this line
    Please Login or Register  to view this content.
    from the ; to ,
    The text file posted in the first post has ;

  9. #9
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Thank you this solves the issue with text and csv. But now when I load the original csv, I face another big issue.. . Sorry to be that annoying
    When I do it manually Data>from CSV/TEXT and import it, it does stay organized in columns. For example
    Colum A/row 1 - Subject
    Colum B/ row 1 -Start date and so one.

    When I do it with the macro it upload it differently.
    Please check photo with name TEST 4

  10. #10
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Here is the phoho
    Attached Images Attached Images

  11. #11
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    329

    Re: Importing data from CSV/TEXT file

    If you look at the workbook I posted you can see why it is writing to where it is.

    You can change that with this line writeToRow = 4, you'll want to change this line as well Sheet1.Cells(2, 4).Value = " with " & (writeToRow - 5) & " records. " or introduce a new variable to count the number of lines read from the text file.

    The code just directly reads the file and posts each row to the sheet. There is no known formatting of the sheet that you're importing the file into.

    edit: typo

  12. #12
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    I changed to line writeToRow =50 and started from row 50, but still it's not in consistent order.
    It should be for example:
    Row 50 Subject, Start date, End Date and so on.. btw it has on each row " for some reason .
    Row 51 X,Y,Z, etc

    At the moment the text its everywhere. I guess its something with rows I need to do but can not figure it out.
    Attached Images Attached Images

  13. #13
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    329

    Re: Importing data from CSV/TEXT file

    I'd have to see the actual file you're trying to import; it seems much different than the example file you uploaded.

    EDIT: you can DM me

  14. #14
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Hi,
    Here is the file
    Attached Files Attached Files

  15. #15
    Valued Forum Contributor
    Join Date
    09-18-2023
    Location
    Geogia, USA
    MS-Off Ver
    365
    Posts
    329

    Re: Importing data from CSV/TEXT file

    This file is nowhere close to the example file. It would take quite a while to create a process to map the data to the cells you want from this file.
    Given the large number of lines without comma separated data (should it be ignored?)

    As stated, the code supplied is for your example you supplied.
    If you want the lines w/o actual data, use an if statement that checks for comma and if there is one then parse the line and write it to the sheet.

  16. #16
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Thank you very much for spending your time. Well I guess, that I will just give up on that since its kind of hard for me.
    Anyway thanks a lot.

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

    Re: Importing data from CSV/TEXT file

    Quote Originally Posted by daneca View Post
    ... Here is the file
    dobro utro daneca ... please provide the appearance (in an xlsx file) of an example result of importing data from a csv (12,csv) file.

  18. #18
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Hi mjr veverka,

    This is how it looks when you go the steps, Data>From TEXT/CSV > Load and it do a table. My idea is to insert the file with a macro button and then create a pivot table.
    Attached Files Attached Files

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

    Re: Importing data from CSV/TEXT file

    Why do you need a macro if you use PQ ?
    It's like trying to transport a couch in a passenger car when you have a pickup truck in the garage ...

  20. #20
    Registered User
    Join Date
    06-07-2023
    Location
    Bulgaria
    MS-Off Ver
    EXCEL 365
    Posts
    36

    Re: Importing data from CSV/TEXT file

    Hi,

    Because I have to make several adjustments and formation after the table is uploaded. I have a separate macro for it.
    My goal was to have Sheet 1 with two buttons. One to import the csv file and the second to do the adjustment, but I guess its not possible

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

    Re: Importing data from CSV/TEXT file

    Quote Originally Posted by daneca View Post
    ... but I guess its not possible ...
    It is possible, but the issue is how stable/constant the structure of the csv file attached as an example is ... e.g.:
    Please Login or Register  to view this content.

+ 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: 4
    Last Post: 02-09-2022, 05:28 PM
  2. Importing data from text file
    By oconee1985 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-16-2021, 05:35 PM
  3. Problem with importing data from text file
    By Frederik1234567 in forum Excel General
    Replies: 3
    Last Post: 12-21-2014, 03:34 PM
  4. [SOLVED] importing data from text file
    By askhari139 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2013, 01:36 PM
  5. Importing a data from a text file - user-defined file
    By DaveSev in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2013, 07:02 PM
  6. Importing data from a text file
    By jbaranski in forum Excel General
    Replies: 3
    Last Post: 08-01-2006, 10:10 AM
  7. Importing data from text file
    By tushar_johri in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-06-2006, 05:19 AM

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