+ Reply to Thread
Results 1 to 40 of 40

Text Parsing in Excel through VBA

  1. #1
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Text Parsing in Excel through VBA

    Dear sir/madam

    I'm wondering is there any way to parse the text in Excel. In a tedious process, this problem is one which is consuming most of the time. I read PDF Form's to Excel files and then process it.

    Problem: I'm reading a Text Field from PDF Form using VBA, to an excel cell, where the data from text field looks like this

    Please Login or Register  to view this content.
    Till now, what I'm doing is, manually copying the required text field and then processing it. On an average I process 70 - 100 files a day.
    It's really pain full doing the same thing whole day.

    What I look in text is, There will be Some File Name (SRDD_6_2_8_8_1_AHM_ITE_WRN_COND.DOC; ), At the END which will have a WORD "GEN" followed with some number (GEN 97)

    Flaws:
    1) No standard naming convention,
    2) No standard file extention, (In this case all are ".DOC" files, it could be ".H ", " . CD " or any thing for that matter)
    3) No limit for number of files (In this case it has 18 files, some time it has 50 to 60 files or even more)

    It would be great, if someone knows, if there is a way to automate the process

    This is what finally I take up on from the text field

    Please Login or Register  to view this content.
    If there is some or the other way to do it, please let me know. please sir please.... Your help would lot my life on repeated tasks.

    Please let me know if you need more inputs.

    Thanks a million in advance

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Text Parsing in Excel through VBA

    Try this code
    Please Login or Register  to view this content.
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button

    You can keep this macro in one file. Then open each file containing the raw data. Run the macro on that file. The output will be in column B.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Thanks for the quick reply...

    Sorry, I forgot to mention one thing, the whole text is contained in Single Cell only (Not in multiple cells), which is continuous in nature
    Last edited by arlu1201; 09-21-2012 at 07:43 AM. Reason: Do not quote whole posts.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Text Parsing in Excel through VBA

    Can you upload a sample excel file?

    To Attach a File:

    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Assuming data in A1
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Hi
    I'm enclosing the sample file, please check
    Parse_Text_File.xls
    Last edited by Cutter; 09-23-2012 at 09:48 AM. Reason: Removed unnecessary quote

  7. #7
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Text Parsing in Excel through VBA

    Try this, I see Jindon already beat me to it though.

    Please Login or Register  to view this content.
    Last edited by mike7952; 09-21-2012 at 08:26 AM.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  8. #8
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    @ jindon

    Nice code, but this is not working fully. This code is modifying only file names which has " ; " and ignoring the rest.

    I have attached a sample file, please look at that
    Last edited by Cutter; 09-23-2012 at 09:49 AM. Reason: Removed whole post quote

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Please Login or Register  to view this content.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Text Parsing in Excel through VBA

    Josie

    if at first you don't succeed try doing it the way your wife told you to

  11. #11
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    @ jindon

    At this line
    Please Login or Register  to view this content.
    it is giving an error "Run Time Error 9 " " Script Out of Range "
    Last edited by Cutter; 09-23-2012 at 09:49 AM. Reason: Removed whole post quote

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Changed a bit.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    @ mike7952
    It had replace only first " ; " with " / " then it had removed all other unwanted convectors like "/" "-" which are before word " GEN ". I had also replaced spaces before and after word "GEN"
    Then text became like this "SCD_6_2_8_8_2_1_ITE_WARN.DOCGEN57"
    Which we need like "SCD_6_2_8_8_2_1_ITE_WARN.DOC/GEN=57"

    Is it possible to get (only) Each File Name (Filename.Doc/GEN=XX) in separately in individual cells in row as I have shown in Excel attachment (which was done manually)
    Last edited by Cutter; 09-23-2012 at 09:50 AM. Reason: Removed whole post quote

  14. #14
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Thumbs up Re: Text Parsing in Excel through VBA

    @ jindon

    Woow.... wonder full... this is what I'm looking for so long...

    Jindon you saved my life a large extent...

    Thank you Jindon Thank You So Much... Thanks to Arlu, Mike & The Forum Admin's...
    Last edited by Cutter; 09-23-2012 at 09:50 AM. Reason: Removed whole post quote

  15. #15
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: Text Parsing in Excel through VBA

    Heres another. Wish I know vbRegExp
    Please Login or Register  to view this content.

  16. #16
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Text Parsing in Excel through VBA

    Quote Originally Posted by Novice_To_Excel View Post
    Woow.... wonder full... this is what I'm looking for so long...

    Jindon you saved my life a large extent...

    Thank you Jindon Thank You So Much... Thanks to Arlu, Mike & The Forum Admin's...
    Unfortunately (for me), I saw this question in the MrExcel forum where the OP posted the question without a link back to this cross-posted question here (someone else posted the link back to here later in thread), so I didn't know the question was already answered here. Anyway, since I developed a solution, and since the solution is different from the one you have (it does not use Regular Expressions, rather, just straight VB coding), I figured I would post it here (assumes your text is in A1 and outputs to B1)...

    Please Login or Register  to view this content.

  17. #17
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Text Parsing in Excel through VBA

    After running Jindon's code, I see you wanted the text split out to individual rows. No problem... here is my code modified to do that (assumes text in A1, outputs list to B1 on downward
    Please Login or Register  to view this content.
    Last edited by Cutter; 09-23-2012 at 09:52 AM. Reason: Removed whole post quote

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    As per your message
    Please Login or Register  to view this content.
    1) (\S+\.[^ ;/\-]{3,4}) ?
    \S+ = Any character(s) other than space

    \. = a period

    [^ ;/\-]{3,4} = 3 to 4 length of any characters other than space, semi-column, forward slush or hyphen.
    It is a Extension part to be checked, so actually {3,4} should be {2,4} as Extension sometimes have 2,3 or 4 characters.

    ? = with or without space

    2) (?:[;/\-]?) ?
    (?:[;/\-]?) = Followed or not by semi-column, forward slush or hyphen and with or without a space.

    3) (GEN) (\d+)
    "GEN" & a space & numbers.

    If the string matches above pattern, it can be stored in matches collection via Execute method.
    Then you can refer each match in matches collection by its index start from 0.

    Please Login or Register  to view this content.
    Now use of Replace method, you can refer each sub-matches in each match in match collection reference to the order of open bracket.
    But if the bracket begin with (?, it will not referenced, I mean ignored.

    So
    $1 refer to "(\S+\.[^ ;/\-]{3,4})" part
    $2 refer to "(GEN)"
    $3 refer to "(\d+)"

    Then "$1/$2=$3" will replace the string with
    $1 & "/" & $2 & "=" & $3

    Understand?

  19. #19
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Thanks Jindon, I got most of it with your help...

    in section two, i.e $2 which is referring to "GEN" can I include some more unique words as "REV" or "CMGEN"

    $2 referring as (GEN or REV or CMGEN)

    then replace if it has any of three words "GEN or REV or CMGEN" with one common word as "GEN" or some other unique word
    Last edited by Cutter; 09-23-2012 at 09:53 AM. Reason: Removed whole post quote

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    That's one of the beauty part of utilizing Regular Expressions.

    Please Login or Register  to view this content.
    Pipe means "or".

  21. #21
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Thanks a lot Jindon

    i have changed first line as you said
    Please Login or Register  to view this content.
    then i have changed next line as
    Please Login or Register  to view this content.
    so that it will replace all three words with word GEN!! and it worked!! thanks Jindon... Thanks a lot...
    Last edited by Cutter; 09-23-2012 at 09:53 AM. Reason: Removed whole post quote

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    No problem and don't forget to mark this thread as "Solved"

  23. #23
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Hi
    I have given every possible version of data that can exist, and modified the code to suit it.

    It processed every line with different parameters and gave result, but except when there are characters like " : " " \ " , these two are also displayed in the processed text lines

    like
    SRFD_3_30_351.d:/GEN=623
    SUHD_7_42_345.doc\/GEN=747

    which are expected to be
    SRFD_3_30_351.d/GEN=623
    SUHD_7_42_345.doc/GEN=747

    Tried to change the code, when I did so, either it is giving error or excluding lines which are needed to be processed..

    Please Login or Register  to view this content.
    Please help me what to do...

    Attaching file for reference
    Parse_Text_FileWithCode.xls
    Thank you
    Last edited by Cutter; 09-23-2012 at 09:54 AM. Reason: Removed whole post quote

  24. #24
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Text Parsing in Excel through VBA

    @ Novice_To_Excel

    Welcome to the forum.

    Please refrain from using whole post quotes. Rule #12 prohibits them. Also, it seems, based on post #16, that you also broke rule #8 regarding cross posting. Please familiarize yourself with the forum rules.

  25. #25
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    @Cutter

    I have just now gone through the Forum Rules. I promise to adhere to them and please excuse me for inconvenience faced.
    Thank you

  26. #26
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Try change to
    Please Login or Register  to view this content.

  27. #27
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    @Jindon

    Thank you, you have helped me lot, literally save so many hours of my work life. Thanks for the time taken and solving my problem ... Thanks you very much...

  28. #28
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Hi

    First I need to thank Jindon and the Forum for their great help. You have made my life easy to a large extent...

    In continuation with the above Problem and Solution,
    I have a doubt... Can we stop the Test Parsing in middle and give the output...

    Like

    The Text Field contains info like this
    Please Login or Register  to view this content.
    With the help of Jindon's code, I'm able to get all 5 file names.
    Please Login or Register  to view this content.
    But, In the above given 5 file names, I would need only first two file names.
    Please Login or Register  to view this content.
    Here the catch is the File extension will be ".ZIP" and will be followed with a word "CONTAINS", from this line there may me any data which we need to ignore - including the file name with extension .ZIP...

    Now the problem is, how to skip the test parsing, when it finds particular pattern like ("FileName.Doc/GEN=5, Contains:......") or word like ("CONTAINS").

    And how to ignore these "," which comes in between,
    I have tried adding "," in ignore list, but it is still showing
    Please Login or Register  to view this content.
    Thank you

  29. #29
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    not sure about "Contains",

    Is it just
    Please Login or Register  to view this content.
    ?

  30. #30
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Fine I will do that part (Contains) manually, those kind are few per day.

    & How to ignore the "," (comma) in the pattern

    I have tried with
    Please Login or Register  to view this content.
    But when I run the code, entire line is being ignored which has " , " (Comma) in between.

    Any suggestions....

  31. #31
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Post all possible patterns of raw data, otherwise everythnig has to be changed time to time.

  32. #32
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Thanks for the patience you have and Sorry for the trouble I gave you... and coming up with new problems added...

    That was only part which I'm left behind in "automating text field processing" part

    I'm glad with the code you have developed for me, Every thing is fine, except two things
    1) Not able to ignore the "," (Comma) which comes in between
    2) Need to skip from the line where the File Extension is ".ZIP" and followed with the word "Contains" (It should continue text parsing if ".ZIP" is NOT followed with the word "Contains"

    Text Field For Example
    Please Login or Register  to view this content.
    Required data from the text field
    Please Login or Register  to view this content.
    Can we make that possible...

  33. #33
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Now it needs to evaluate line by line
    Please Login or Register  to view this content.

  34. #34
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Better I go with the this code itself, may be it is ignoring few lines, but it is still fine to work with, and most of the work will be done automatically.

    The earlier code which you have developed to skip from Zip files, which is still not ignoring "," (Comma)
    Please Login or Register  to view this content.
    If it was just
    Please Login or Register  to view this content.
    it is ignoring "," Comma, but not ignoring "\" Slash

    If it is this
    Please Login or Register  to view this content.
    it is ignoring "\" Slash,
    And file names which have "," are completely ignored

    All characters which are expected are... (, : ; / \ - )

    Can we ignore all the above characters including "," & "\" with single code...
    Last edited by Novice_To_Excel; 09-27-2012 at 12:59 PM.

  35. #35
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Change to
    Please Login or Register  to view this content.

  36. #36
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Now the code is doing as we want it, But, when ever there is a number like 10, 20, 30, 40,.... it is resulting ' 0 ', and it is fine in other cases numbers like 6, 27, 59, 123,...

    Text Field is Like
    Please Login or Register  to view this content.
    Result is like
    Please Login or Register  to view this content.
    Should we need to change section 3 i.e " (\d+) " to accept any number. (Numbers limited to 1 to 10000)

  37. #37
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    It is working here

    result I got

    DRFD_6_2_8_8_1_AHM_ITE_WRN_COND.DOC/GEN=10
    TGHD_6_2_8_8_2_1_ITE_WARN.DOC/GEN=20
    HY_GTM_005_IT_SW_B4.ZIP/GEN=40
    HU_YHM_005_IT_SW_B57.DOC/GEN=60
    DRFD_6_2_8_8_1_AHM_ITE_WRN_COND.DOC/GEN=12
    TGHD_6_2_8_8_2_1_ITE_WARN.DOC/GEN=226

  38. #38
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Thanks Jindon

    Got the mistake what i have done, and corrected it by looking at your code again. Now it is doing it's part seamlessly...

    I understood most most of your code and was able to customize it to a large extent.

    But, I think, I'm missing the logic behind these things, may be their might be some pattern or logic in quoting them
    [^ ;:/\-,\\]
    (?:[ ,;:/\\\-])?
    (?:[ ,=])?

    However, I grateful to you, You have helped me a lot and made my life easy... Thank you so much...

  39. #39
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Text Parsing in Excel through VBA

    Now it's time to mark this thread as "Solved".

  40. #40
    Registered User
    Join Date
    09-21-2012
    Location
    bangalore
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Text Parsing in Excel through VBA

    Thanks Jindon, Marked it as "SOLVED"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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