+ Reply to Thread
Results 1 to 29 of 29

Text to Excel Conversion using VBA or Macro

  1. #1
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Text to Excel Conversion using VBA or Macro

    Hi,

    I have a text file which I would like to convert it into excel sheet using VBA or Macro. The issue is that the data I get on daily basis is in text file and is too huge as it is a report of some software application. I am sending you a sample report and how it will look after conversion for a better overview of the problem. As I mentioned above I need to convert the text file into excel worksheet.. I need a VBA that will eliminate all the irrelevant data from the text file and pick only the required data and paste it in the relevant Excel spreadsheet column.. Ps assist.. Please help me with a VBA code for the same.

    Thanks in Advance..
    Attached Files Attached Files
    Last edited by msb15; 01-24-2016 at 12:27 AM.

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Text to Excel Data Conversion using VBA

    Your checking.xlsx file doesnt really provide any help though, how do you want the data formatting?

    If it was me needing this then Id put all the possible codes and headings along the top (I assume theres more possible fields than just those in the example file).

    Then read the file in line by line checking if the 4th character is a :, if so assume its a header line and cycle through the headers until you find the matching one.
    The keep pulling in lines from the file until you hit another 4th char of : When you get that output the data to the relevant column

    If you get the words "BLOCK 4" increment your output row and continue on to start a new record
    If someone has helped you then please add to their Reputation

  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Text to Excel Data Conversion using VBA

    If you do a search on "Line Input #" you will find ways to read through the text file. You will also see some examples where the line is parsed and stored on a spreadsheet.

    Hope this gets you started.


    click on the * Add Reputation if this was useful or entertaining.

  4. #4
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Data Conversion using VBA

    sir can you please show it with doing it for on value field let' say 20: .. where ever 20: comes in my text file i want the value in a "20:" column..
    please just do it for one.. i will do the rest

    I hope you understood what I'm trying to do here.

    I'm trying to take all the text data in BLOCK 4 and paste it in field like "20:" and others.

    Sorry for my bad english.

  5. #5
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Text to Excel Data Conversion using VBA

    You will find all you need in this post: http://www.excelforum.com/excel-gene...using-vba.html

    This post has a post about using the open file. I particularly like the one from tony h http://www.excelforum.com/excel-gene...le-issues.html

  6. #6
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Data Conversion using VBA

    Ok one more question..
    I have data like this:
    20: Sender's Reference
    C0000000000000

    in this Sender's Reference text is not needed but the C0000000000000 is the required data..

    so when i will target 20:.. i will have to code about go to next line and fetch this C0000000000000.. and i don't know how to do this..

    just help me with this.. or as i asked please do sort 1 field out for me.. i will owe you for this.

  7. #7
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Text to Excel Data Conversion using VBA

    First you have to decide how to determine which lines to keep:
    Maybe they are in a regular format and you need to keep every 2nd or 4th line. Maybe all lines where the 3rd,4th and 5th characters are zero.

    Then apply the test after each line input to either save the value to the spreadsheet or ignore it and do the next line input

  8. #8
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Data Conversion using VBA

    Can you please provide the code for some values retrieving from the text document. That would be great help sir.
    Also I have updated the Sample Text and Excel File. Please look into it then you will exactly know what I'm trying to tell you sir. Thanks.
    Last edited by msb15; 01-23-2016 at 09:43 PM.

  9. #9
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    See attachment as well.

    Allows multiple file selection.

    Not much error checking since you only provided one SHORT text file.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    David
    (*) Reputation points appreciated.

  10. #10
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Sorry sir for a childish question but the Button is not working. Also I have added more data in sample file. A little more help and it will be done. You're genius. Thanks.

  11. #11
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    but the Button is not working.
    On the sample file I uploaded? Make sure you enable any yellow security button that comes up.

    If you have the code stored in a different location than Module1, you have to go into design mode, double click button and point to wherever the code is.

  12. #12
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Button is working now. But the problem is its showing the Sender's Reference in Exponential form. Let's say if the value is "2016010800145698" its showing "2.01601E+15". Secondly, its showing only Date from the 32A and the amount value of 33B.

    Can you please look in the sample excel file please? That would be great help. I tried editing your code but I'm getting multiple errors whenever I try to edit.
    Last edited by msb15; 01-23-2016 at 11:32 PM.

  13. #13
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    ts showing "2.01601E+15"
    Select the column, Ctrl-1, select General.
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Great work sir. Thank you so much.


    Last thing, sir please look at the sample_text_file_final.txt‎ and make code compatible to it, it has the same data but some data is stopping the script from moving ahead the receiver option.. you'll have to do a little editing and it will be done. One last favour, if you're available.
    Last edited by msb15; 01-24-2016 at 12:30 AM.

  15. #15
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Sir the code is not going further the sender and receiver..

  17. #17
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Sir the code not going further the sender and receiver field and its only retreiving the first sender and receiver. Sorry wrote it twice.
    Last edited by msb15; 01-24-2016 at 12:45 AM.

  18. #18
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    it's working on the sample file.

  19. #19
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    sir actually its not able to move further due to some irrelevant data which is not needed. Please look at sample_text_file_final

  20. #20
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    That's the one I was referring to.

  21. #21
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    No sir you are referring to the other file. Please look at sample_text_file_final.txt file. Just a few changes are needed, you have 99% of the work sir.

  22. #22
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    In the future, attach files to the most recent message, not the first one.

    Please Login or Register  to view this content.
    Last edited by Tinbendr; 01-24-2016 at 01:53 AM.

  23. #23
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,726

    Re: Text to Excel Conversion using VBA or Macro

    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  24. #24
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Sorry for cross posting..

    Mr. Tinbendr you're the best.. Thank you so much for your help and support.

    Please tell me if I can do anything for you in return of this favour. I have already added excellent reputation. Live long sir.

  25. #25
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Mr. Tinbendr A little Question.. I'm trying to add the Transaction Ref. column also and the code I did to add that column is this,

    Please Login or Register  to view this content.
    Its fetching the Transaction Ref correctly but the problem is that its fetching like this "Transaction ref. = XXXXXXXXXXXXXXX1" and I only want "XXXXXXXXXXXXXXX1" part.. what should I add to remove the Transaction ref. part and just get the real reference value sir..
    Last edited by msb15; 01-24-2016 at 02:14 PM.

  26. #26
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    Since this is a fixed/defined length line, I think the mid function will serve better.

    The format will always be 'Transaction ref. = (a 17 digit number)'.

    So, we just need to define the parameters of the mid function.

    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Alright sir.. Small small fixes sir I only want USD or currency short in Currency Column not the whole "USD (US DOLLAR)" what should I do for that.

    Also the script you gave is working great on sample as It has only 2 transactions but when I tried it on 100 transactions text file, it omitted some transactions. So to check what's wrong in it I commented the Case 71A part and all the transactions started to appear. Can you please tell me what actually is wrong there that with Case 71A enabled less transactions are retrieving.

    Another problem, the inter bank settlement amount which is the most important field is sometimes becoming empty after retrieving data from text document. I think the problem is size of the data type because its working great for values like "930.85" but showing nothing for values like "1,161,468.50".

    Thank you in advance sir

  28. #28
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Text to Excel Conversion using VBA or Macro

    I guess I was more tired than I thought last night.

    I dropped the 'line' off of 'Line Input #FF'. This made the variable read UP TO a comma. Hence this is why the amount wasn't returned correctly.

    I also went back to using the Split on the records that have a colon/hash make in them. It's a little more robust.

    Since I had to back up a little with the code, be sure to check previous 'fixes' to ensure the output is correct.

    Please Login or Register  to view this content.

  29. #29
    Registered User
    Join Date
    12-12-2015
    Location
    Karachi, Pakistan
    MS-Off Ver
    2016
    Posts
    46

    Re: Text to Excel Conversion using VBA or Macro

    Everything is working fine now. Thank u so much.
    Last edited by msb15; 01-28-2016 at 08:18 AM.

+ 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. Data conversion from excel to txt
    By ramanan256 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-27-2010, 02:40 AM
  2. text to excel conversion
    By shahcu in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-14-2007, 03:45 PM
  3. Replies: 2
    Last Post: 06-12-2006, 08:00 PM
  4. PDF to Excel Data Conversion Software
    By ExcelTip in forum Tips and Tutorials
    Replies: 0
    Last Post: 05-10-2006, 06:06 AM
  5. Excel Text Conversion Problem
    By Michael D in forum Excel General
    Replies: 0
    Last Post: 03-13-2006, 08:30 PM
  6. Automatic data conversion in Excel
    By Dejan in forum Excel General
    Replies: 2
    Last Post: 07-26-2005, 05:05 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