+ Reply to Thread
Results 1 to 10 of 10

VBA noob. Need help with reformatting compromised data

  1. #1
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

    Post VBA noob. Need help with reformatting compromised data

    I have been tasked with reformatting a very large report. The problem is, the source file did not maintain its integrity when the user transferred it to Excel. Now, there are almost 7000 individual units that will need to be reformatted manually. Rather than working each individual cell, I have been attempting to create a macro to save me 60+ hours of work, however I haven't had much luck. Let me explain what I'm trying to do:

    The reports consists of upwards of 150,000 rows, all under column A. This is the basic layout throughout the reports:

    A
    1 RUN DATE ##/##/## NAME OF BUSINESS PAGE #
    2 RUN TIME ##:##
    3
    4
    5 FIELD 1: #### DESCRIPTION
    6
    7
    8 FIELD 2: DESCRIPTION
    9
    10
    ...
    22 FIELD 7: DESCIRIPTION
    23 DESCRIPTION CONTINUED
    24 DESCRIPTION CONTINUED
    25 DESCRIPTION CONTINUED
    26
    27
    28
    29 FIELD 1: #### DESCRIPTION
    ... and so on.

    Additionally, "Field 7" will always begin with the text "DOCUMENTATION:". Field 7 may be anywhere from 1 - 7 different rows. Sometimes, if the page ends immediately after FIELD 7, the time/date stamp will be placed before FIELD 1. So, Field 7 will be immediately followed by either "EDIT:" or "RUN DATE". What I need to do, is take everything in "FIELD 7", for each item, and paste it into Column A on sheet 2 or in a column on that page. Either would be fine. However, I am having trouble creating a macro to perform this function and was hoping someone might be able to lend me some/plenty of assistance.

    The end result of this report would ideally look like this:
    A
    1 "FIELD 7 DESCRIPTION"
    2 "FIELD 7 DESCRIPTION"
    3 "FIELD 7 DESCRIPTION"
    ...
    7000 "FIELD 7 DESCRIPTION"

    I appreciate any help you might be able to lend. Let me know if there is anything that isn't clear. I'll continue to experiment with this and provide updates as they come.

    Thank you.
    Last edited by mea02300; 06-24-2013 at 02:12 PM. Reason: Additional information

  2. #2
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: VBA noob. Need help with reformatting compromised data

    See if this does what you need. Results are stored in Sheet2.
    Add this code to the module of the worksheet that contains all
    the text.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA noob. Need help with reformatting compromised data

    Hmm. I'm getting a "Type Mismatch" error.

  4. #4
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: VBA noob. Need help with reformatting compromised data

    Could you try this and see if the error goes away.
    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    and change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA noob. Need help with reformatting compromised data

    I made the change, but I'm still getting the type mismatch.

    I'm not sure if this matters, but the data that is being pulled will be a mixed bag of upper/lower case, numbers, as well as symbols (eg. '/*>:<&%). I can't see any other commanalities.
    Last edited by mea02300; 06-24-2013 at 04:31 PM.

  6. #6
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: VBA noob. Need help with reformatting compromised data

    First I would try cleaning the data of all non-printable characters.
    In Cell B1, type the formula "=Clean(A1)" and copy this down to the
    last data row. Then copy and paste column B as values, and then replace
    column A with the cleaned-up data in column B.

    Then if that still gives the type-mismatch error, you could look for the failure point.
    Add this line
    Please Login or Register  to view this content.
    Insert this line just after the "For j =..." line:
    Please Login or Register  to view this content.
    Run the code again and see what the value is in column E of your worksheet.
    Let's say it reached 935 before the macro fails. Run the macro again and see if
    you get the same result in column E. If you do, then put this code just inside the For loop:

    Please Login or Register  to view this content.
    and set a debug break point on the line "lngTest = lngTest".
    Step through the code in debug and identify the line of code that fails and
    what the value of the variable j is at that point, which may give some
    insight about the offending cell of data.
    Stop the macro and run it again to the point where you go into debug, then step
    through to the point just before the failure and examine in a Watch window the value
    that is in the cell being read. It may be data that does not want to be treated as text.
    Last edited by xLJer; 06-24-2013 at 05:42 PM.

  7. #7
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA noob. Need help with reformatting compromised data

    Thank you very much. I'll give that a try first thing tomorrow morning and let you know how it worked.

  8. #8
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA noob. Need help with reformatting compromised data

    Ok I walked through all of these steps. For the IngTest, the value I'm getting in Column E is "1". I attempted to walk through with the debugger, but it looks like it's getting stuck in a loop. I'm not sure if I did something wrong here. Either way, here is a copy of the data I'm working with, with the sensitive data altered. Maybe that will clear things up. Again, what I'm attempting to do is copy everything after "Documentation:". The sample includes roughly 7000 items like these two. Report sample.xlsx

  9. #9
    Forum Contributor
    Join Date
    07-27-2012
    Location
    California, USA
    MS-Off Ver
    Excel 2003
    Posts
    198

    Re: VBA noob. Need help with reformatting compromised data

    Try this code that I revised to work with the sample data
    in your attachment. Results will be stored in the same sheet as your data,
    in column G.

    If you get the same type mismatch error and no text appears in column G, put a debug
    break point on the line that assigns a value to lastRow. Run the macro and
    see if the value for lastRow is the same as the ending data row in column A.

    Please Login or Register  to view this content.
    Last edited by xLJer; 06-26-2013 at 12:04 PM.

  10. #10
    Registered User
    Join Date
    06-24-2013
    Location
    Coal Township
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: VBA noob. Need help with reformatting compromised data

    Thank you very much. That worked like a charm. It effectively turn 60+ hours of work into just under 5. I really appreciate it.

+ 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