+ Reply to Thread
Results 1 to 12 of 12

Open text file and store specific columns using arrays

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Open text file and store specific columns using arrays

    Hello everyone
    I have Sample text file (in fact I would deal with large number of text files ) >>
    Manually I copy the text file data and put it into sheet then select column A (Where I put this data) >> go to Data tab >> Click "Text to Columns" and split data by comma
    Then delete the first four columns and the header row...
    There are four columns remaining //


    After that I am using this code to have these results in specific way
    Please Login or Register  to view this content.

    ** I need not to deal with the excel file when manipulating data .. I just need to deal directly with the text file and do all these tasks using arrays
    Mr. Karedog has helped me in similar issue .. but this is different a little

    Thanks advanced for help
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by YasserKhalil; 03-22-2017 at 04:44 PM.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,446

    Re: Open text file and store specific columns using arrays

    Hi Yasser,

    Power Query will do this split and remove columns before you get it into Excel. I just don't know if PQ is available/allowed in Egypt yet.
    https://www.microsoft.com/en-us/down...4-42a65920901d
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open text file and store specific columns using arrays

    Thanks a lot for reply
    I don't have enough knowledge with Power Query and I need to do that as it is part of large project .. and I have about 2000 text files
    When I post the thread I just take the idea of how to solve it with one text file .. After that I try to adopt it to the other text files using loops
    Best Regards

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,446

    Re: Open text file and store specific columns using arrays

    Hey Yasser,

    Find attached your data, I typed in, and did what I suggested. It took about 3 minutes to build this PQ answer. Here is what I did:
    1. Pulled in your data into Power Query. PQ looks like Excel in rows and columns but is a pre-process before it gets into real Excel
    2. Added a Index column, so I could UnPivot all your data.
    3. Filled in all the nulls in your table with zero instead of null.
    4. Unpivoted the data in PQ
    5. Load and Save it back to column K of the workbook.

    NOW - If you were to give me a new workbook OR Text file, I'd simply change the first step of where the source .txt file is located and it would do the above steps.
    AGAIN - If I told it to do all text files in a folder (your 2000) it would do it for all 2000 files all at one time.

    See my PQ steps in the attached (You will need to have installed the add-in. Microsoft has a tool in Excel to do all your work. You simply need to learn it. It is much easier than VBA.
    PQ Transpose for YasserK.xlsx

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open text file and store specific columns using arrays

    Thank you very much Mr. Marvin for your great efforts
    It didn't work for me. Incompatible warning with current version and even if it worked it won't suit my needs as I have told you that it is just a part of another large code ...
    So All what I need to get the results you got in J & K in an array .. Not even need to populate it as it will be related to another part of code
    Thanks a lot for your great help. I appreciate that a lot

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open text file and store specific columns using arrays

    Maybe :
    Please Login or Register  to view this content.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open text file and store specific columns using arrays

    Thank you very very much Mr. Karedog for this awesome solution
    Just little point : In the first column of results I noticed the last is 235 and I need it to be 365 ... I tried to change that part
    Please Login or Register  to view this content.
    to be
    Please Login or Register  to view this content.
    But I got the same results
    What could I change to fix that part?

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open text file and store specific columns using arrays

    235 is a correct number.
    If you open Sample.txt and cut paste to Excel, you will notice that the last row is 48.
    We discard first row (header) by deleting this row, the result is 47.
    Number of columns want to retrieved is 5.

    So total = 47 * 5 = 235 (which is the result of macro).

    From what calculation you come to this 365 number ?

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Open text file and store specific columns using arrays

    Anyway, if you want to force the output to a certain number of rows, the function is added with one extra parameter : nRowsOutput
    Please Login or Register  to view this content.

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open text file and store specific columns using arrays

    Thank you very very much Mr. Karedog for these charming solutions. You are life savior
    Saying thank you is NOT ENOUGH at all ...
    Best and kind regards

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open text file and store specific columns using arrays

    Hello Mr. Karedog (I am so sorry for disturbing you in these similar issues)
    I have different structure in the following sample text file >>
    What I am doing manually here is to copy the text file to worksheet and I got 16 columns
    I want first to skip the first two rows (headers) and keep only four columns of these 16 columns (column A & E & I & M)
    Look at this snapshot to see green parts are what I want to keep .. in one array with four columns

    I don't want public procedure for that.. Just regular macro
    Thanks a lot for great and wonderful help in that complicated issue
    Best Regards
    Attached Images Attached Images
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Open text file and store specific columns using arrays

    Hello
    After studying your awesome code I could figure it out to suit my new sample text file
    I welcome any fixes
    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. [SOLVED] Open CSV file using vba excel without change text to columns automatic
    By alcalina in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 09-04-2024, 07:55 AM
  2. [SOLVED] Big Excel Data File ( 3 Deep columns ) To Text File To 3 Big VBA 1 Dimensional Arrays
    By Doc.AElstein in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-08-2017, 10:47 AM
  3. [SOLVED] Open text file and copy specific rows into spreadsheet column
    By larsahl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2014, 07:02 AM
  4. Open other excel files with specific text in the file name
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2013, 04:45 AM
  5. VBA to open .CSV file and format Columns A and C as text
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-12-2012, 12:56 PM
  6. Perform macro "on open" specific file- store macro in Personal Macro Workbook?
    By thompssc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-17-2012, 12:38 PM
  7. Open text file to specific workbook
    By WasWodge in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2011, 05:44 PM

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