Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-20-2014, 12:03 PM
winmaxservices2 winmaxservices2 is offline Need code to Parse  and arrange data based on heading Windows 8 Need code to Parse  and arrange data based on heading Office 2013
Novice
Need code to Parse  and arrange data based on heading
 
Join Date: Dec 2014
Posts: 2
winmaxservices2 is on a distinguished road
Default 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.
Attached Files
File Type: xlsx Test.xlsx (9.5 KB, 15 views)
Reply With Quote
  #2  
Old 01-01-2015, 01:04 AM
excelledsoftware excelledsoftware is offline Need code to Parse  and arrange data based on heading Windows 7 64bit Need code to Parse  and arrange data based on heading Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

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
Reply With Quote
Reply



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
Need code to Parse  and arrange data based on heading Creating a table that automatically updates based on entries of a heading in the document cahphoenix Word 3 10-29-2014 01:11 PM
Need code to Parse  and arrange data based on heading 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

Other Forums: Access Forums

All times are GMT -7. The time now is 11:12 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft