+ Reply to Thread
Results 1 to 11 of 11

Find a specific string

  1. #1
    Registered User
    Join Date
    04-19-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2002-2003
    Posts
    6

    Cool Find a specific string

    Hi All
    I am a beginner in VBA, I know little bit but I learn every day something new and that is very nice.
    I am looking for help or code to solve that challenge.

    On a regular basis I receive a very large Excel 2002 Sheet, with +/- 16 000 lines (yes +/-16 000 lines) with data coming from a main frame.
    The data have to be re-organized in order to do a huge mail merge.
    I have witten few macros that help me to do the portion of the job but the following step is more difficult for me.

    I have to find strings like "CITY XX" or "SMALL TOWN IN CANADA XX", where XX is the provincil code, within 4 possible columns, say E, F, G, H. Range is E2 to H 16000.

    Then, once found, these strings (City name and province code) must be copied on the same line but on column K, then deleted from its original location (E, F, G, H)

    To be more precise, what I search is one or more words but with a Province Code, e.g. OTTAWA ON, VANCOUVER BC, CALGARY AB, CORNER BOOK MB, SAINT REMI DE LA LAUBINIERE QC, etc. The provincial code is important because ONLY this kind of strings must be copied on K regardless the number of words for the city/village name before and spaces.

    Note the space between the name of the city and the Province code (2 letters only). Any space in the City name is not important.
    In the columns E, F, G, H, there are other information but only the "CITY XX" format must be moved to K.
    If the information contents numbers, it must be ignored, even if there is a "Province Code" after.

    So the following must be ignored:

    - TRANS CANADA HIGHWAY (there is not a space and a Province code)
    - 123 Main NW (there is a number)
    - Room 34 (there is a number)
    - 34645654 SK (there is a number)
    -GENERAL DELIVERY (there is not a space and a Province code)
    - anything not formatted like "CITY XX" or "SMALL TOWN IN CANADA XX"

    Hoping it is clear enough ...
    Any advise, sample code, help would be very very appreciated.

    Alain Kadlec
    Ottawa.
    Last edited by AK69; 04-26-2011 at 12:18 PM. Reason: Solved -

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Find a specific string

    AK69,

    Welcome to the Excel Forum.


    To get the most precise answer, it is best to upload/attach a sample workbook (sensitive data scrubbed/removed) that contains an example of your raw data on one worksheet, and on another worksheet your desired results.

    The structure and data types of the sample workbook must exactly duplicate the real workbook. Include a clear and explicit explanation of your requirements.

    To attach your workbook, scroll down and click on the Go Advanced button, then scroll down and click on the Manage Attachments button.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Registered User
    Join Date
    04-19-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2002-2003
    Posts
    6

    Smile Re: Find a specific string

    Hi
    Attached is sample data on a test Excel file. BAsically all data in blue must be moved/copied to the corresponding cell in the column K and the original data deleted.
    Hope this will help. Thanks a lot in advance.

    Alain Kadlec
    Ottawa
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Find a specific string

    AK69,

    Thanks for the workbook.


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
    2. Open your workbook
    3. Press the keys ALT + F11 to open the Visual Basic Editor
    4. Press the keys ALT + I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
    7. Press the keys ALT + Q to exit the Editor, and return to Excel
    8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


    Please Login or Register  to view this content.

    Then run the FindCityPC macro.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find a specific string

    Hello Ak69,

    Here is another version. This will validate each code before the data is copied over. This macro has been added to the attached workbook.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    04-19-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2002-2003
    Posts
    6

    Re: Find a specific string

    Hi
    First of all, I whish to warmely thank Stanley and Leith for their effort and generosity. I am amazed; you really deserve something more that just warm thanks. I can see that in few seconds I could save 3 days of very boring work and then be able to focus on what is very important for our clients who were just been granted for a disability benefit. The code you have written seems to be very complex et tricky.

    I tried both codes. The code from Stanley dosen't segregate very well the address with number, like 123 MAIN ST, probably because there is a 2 letter suffix at the end and stop at line 147, out of 16000...But it is a very good start.

    The code from Leith seems to be more accurate but when a Province suffix is not valid (not included in the validation tool in the code, which I have completed with more codes) - see Cell F12 in the attached sheet, the space in the column K is "taken" by the next City name. So, the result is offset. That may cause a problem should several suffixes are wrong.
    This kind of error could be frequent since the suffix can be written in English or in French by the clerks (which is the case for F12), but for postal issues the suffixes are in English only.
    I have attached the Sheet with the "new" validation list. If you can fix that I will be not fare from the seven heaven
    Thanks again to you.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find a specific string

    Hello AK69,

    I have updated the macro to keep the addresses in the same row and if an invalid city code is present then the cell in "K" is left empty. Here is the macro which has been added to the attached workbook.

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Find a specific string

    AK69,

    With a different approach, and faster.

    Detach/open workbook FindCityPCv2 w PC array - AK69 - EF773089 - V2 - VP - SDG13.xls and run macro FindCityPCv2.


    If you want to try the macro on a different workbook (with your raw data on worksheet Sample Data):


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    Please Login or Register  to view this content.

    Then run the FindCityPCv2 macro.

  9. #9
    Registered User
    Join Date
    04-19-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2002-2003
    Posts
    6

    Re: Find a specific string

    Thanks a lot to both of you. You have done an amazing work; I am really impressed. I am going to play with the code(s) to see the result on several raw sheets. This code could be usefull for many other people too, mostly in the United States where similar issues may occur. I will let you know after Easter break what I have found. Alain K

  10. #10
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Find a specific string

    AK69,


    This updated macro, instead of looping thru each separate column it uses a rng, and, is faster because it only goes thru the PC array one time.


    Detach/open workbook FindCityPCv3 w PC array VP rng - AK69 - EF773089 - SDG15.xls and run macro FindCityPCv3.


    If you want to try the macro on a different workbook (with your raw data on worksheet Sample Data):


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


    Please Login or Register  to view this content.

    Then run the FindCityPCv3 macro.

  11. #11
    Registered User
    Join Date
    04-19-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2002-2003
    Posts
    6

    Re: Find a specific string

    Hi All
    I have tested both codes intensively on real raw databases and both codes are doing a real great job. Stanley’s code is slower but more accurate by segregating better the province suffixes.
    Leith’s code is much faster but let longer suffixes to be move/copied to column K (On, Ont, Ontario…). That is not a big deal because the suffixes are to be removed at a final stage.
    So both codes are excellent and help a lot to save a lot of time in doing this boring semi annual work. I do not know how I could thank you enough.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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