#1
|
|||
|
|||
Need code to Parse and arrange data based on heading
HI,
Code needed to parse or arrange data in to respective column based on field name.Please find the attachment. |
#2
|
|||
|
|||
This was actually a lot of fun and I am pretty happy with the result.
Copy and paste this code into a module and then run it. Remember that all VBA code cannot be undone so be sure to save your workbook and have a backup before running. this code is written with the headers being on row 2. If this needs to change you can change the value of the HeaderRow where it says 'Set the references Code:
Option Explicit Sub ParsePropertyData() 'Takes each value in column A and manipulates the string to contain 'the proper delimiters. Cleans the string up of extra spaces and 'goes through the headers and stores them in an array to search out Dim CheckString As String, NewString As String, Col As Integer Dim CheckRow As Integer, x As Integer, v As Variant, ParseString As String Dim Header() As String, LastRow As Integer, LastCol As Integer Dim HeaderRow As Integer, StartChar As Integer, EndChar As Integer 'Set the references HeaderRow = 2 LastCol = Range("A" & HeaderRow).End(xlToRight).Column LastRow = Range("A" & HeaderRow).End(xlDown).Row 'Set the headers in an array ReDim Header(2 To LastCol) As String For x = 2 To LastCol Header(x) = Cells(HeaderRow, x).Value Next x 'Format and parse each data row For CheckRow = HeaderRow + 1 To LastRow CheckString = Range("A" & CheckRow).Value 'Format the string CheckString = Replace(CheckString, ": ", ":") For Each v In Header If InStr(1, CheckString, v) Then CheckString = Replace(CheckString, v, ";" & v) End If Next v 'Remove the first semi-colon If Left(CheckString, 1) = ";" Then CheckString = Mid(CheckString, 2) End If 'Convert to a new string to check NewString = WorksheetFunction.Trim(CheckString) 'Parse the data For Col = 2 To LastCol If InStr(1, NewString, Header(Col) & ":") Then StartChar = InStr(1, NewString, Header(Col) & ":") EndChar = InStr(StartChar, NewString, ";") - 1 If EndChar <> -1 Then ParseString = Mid(NewString, StartChar, EndChar - StartChar) Else ParseString = Mid(NewString, StartChar) End If 'Put the result in the cell Cells(CheckRow, Col).Value = Mid(ParseString, InStr(1, ParseString, ":") + 1) End If Next Col Next CheckRow End Sub I tested this on your sample and it appears to work 100% a couple of notes though. On your attachment you have 2 headers that will need to be changed for this to work and continue to work in the future. Column B said Rent Price: Needs to be Rent Price (no colon) Column M said Property should be Property age Let me know how it works out or if you have any other questions. Thanks |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Retrieving data from data base based on text selection | capitala | PowerPoint | 0 | 12-10-2014 08:10 AM |
Creating a table that automatically updates based on entries of a heading in the document | cahphoenix | Word | 3 | 10-29-2014 01:11 PM |
Vba macro code for grouping the data based on 2 hours time | dharani suresh | Excel Programming | 5 | 04-29-2014 03:25 AM |
Re-Arrange Generated Data | flds | Excel | 4 | 06-29-2012 08:17 AM |
Modify vba code to print based on name in the InputBox | OTPM | Project | 0 | 05-25-2011 02:03 AM |