MS Office Forum / Excel / New Users / January 2008
Macro To Amend Data In A Column
|
|
Thread rating:  |
Saruman - 09 Jan 2008 16:21 GMT I hope that someone can help me with a problem I have inherited with an Excel 2003 file.
When the file was created, a column was sized and then the data was made to fit the column by adding spaces and using word wrap to align the data on multiple rows instead of using the Alt+Enter. I now need to remove all the extra spaces and replace them with Alt+enter.
There are currently approx 1800 rows, some of which contain no data and are used as separators between each section. The data is in Column H from Cell H3 downwards. The data is the name of a data attribute followed by a number, which is the unique reference number in our system of that attribute.
eg Estimated Repair Time 508 Repair Action Description 505 Repair action Code 1045
I manually counted some of the spaces and they can range from 15 to 35 of them between each data attribute.
Can any one please assist with a macro to remove all the extra spaces(Char32) and replace them with a single Alt+enter(Char10)? I am an experienced user of Excel with little programming knowledge, but do know where to place the macro code. I have tried using ASAP Utilities to carry out the task but there is still a lot of manual work required afterwards and I am sure this could be done more efficiently with a macro.
Thanks in advance.
Gord Dibben - 09 Jan 2008 17:05 GMT Try this macro on the selected column.
Sub TRIM_EXTRA_SPACES() Dim Cell As Range For Each Cell In Selection If (Not IsEmpty(Cell)) And _ Not IsNumeric(Cell.Value) And _ InStr(Cell.Formula, "=") = 0 _ Then Cell.Value = Application.Trim(Cell.Value) Cell.Replace What:=" ", Replacement:="" & Chr(10) & "", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ ReplaceFormat:=False Next End Sub
Gord Dibben MS Excel MVP
>I hope that someone can help me with a problem I have inherited with an >Excel 2003 file. [quoted text clipped - 24 lines] > >Thanks in advance. Saruman - 09 Jan 2008 19:03 GMT Gordon,
Thanks for a quick reply. This actually removes ALL the spaces and leaves a single word or number on each line.
I need to have the whole description and the number on a single line as per the data in the example.
Thanks.
Saruman --------------------------------------------------------------------------- All Outgoing Mail Scanned By Symantec Antivirus 10 ---------------------------------------------------------------------------
> Try this macro on the selected column. > [quoted text clipped - 41 lines] > > > >Thanks in advance. Gord Dibben - 09 Jan 2008 20:22 GMT I guess I did not understand your original post and example and I might continue to not understand.
I thougfht you wanted extra spaces removed, which my macro does.
It also adds a linefeed between each word which I assume you didn't want.
Is this all in a single cell along with extra spaces to achieve proper spacing for wordwrap?
Estimated Repair Time 508 Repair Action Description 505 Repair action Code 1045
And you want the same thing only with extra spaces removed and linefeeds after 508 and 505?
I would certainly have to think about that code for a while.
Hopefully one of the brighter lights can help.
Gord
>Gordon, > [quoted text clipped - 62 lines] >> > >> >Thanks in advance. Saruman - 09 Jan 2008 21:21 GMT Gord,
Apologies for the misunderstanding.
The current data in a single cell is one long string of characters with many spaces between the start of each name.It is like the example below. The Column width was then adjusted so that the Names lined up on the left side of the cell with a wrap text added that made the data appear to be on 3 lines..
Estimated Repair Time 508 Repair Action Description 505 Repair action Code 1045
I need it to look like the original data example also in a single cell so there is the Data Attribute Name and the Number on one line with normal single spaces between the text and the number, the multiple extra spaces between the number and the next name would need to be removed and a Line Feed would need to be added, such as the Alt+Enter function in Excel.
Estimated Repair Time 508 Repair Action Description 505 Repair action Code 1045
If you are unable to assist, then thank you for your efforts. -- Saruman
--------------------------------------------------------------------------- All Outgoing Mail Scanned By Symantec Antivirus 10 ---------------------------------------------------------------------------
> I guess I did not understand your original post and example and I might continue > to not understand. [quoted text clipped - 87 lines] > >> > > >> >Thanks in advance. Dave Peterson - 09 Jan 2008 21:46 GMT I _think_ that this is what you want.
But it does depend on finding a number that is followed by a space character.
Select your range to fix and try this macro. Try it against some test data before you trust it!
Option Explicit Sub testme()
Dim myCell As Range Dim myRng As Range Dim myStr As String Dim cCtr As Long Set myRng = Nothing On Error Resume Next Set myRng = Intersect(Selection, _ Selection.Cells.SpecialCells(xlCellTypeConstants)) On Error GoTo 0 If myRng Is Nothing Then MsgBox "Please select a nice range" Exit Sub End If For Each myCell In myRng.Cells myStr = myCell.Value myStr = Replace(myStr, vbLf, " ") myStr = Application.Trim(myStr) For cCtr = Len(myStr) To 2 Step -1 If Mid(myStr, cCtr, 1) = Space(1) Then If IsNumeric(Mid(myStr, cCtr - 1, 1)) Then Mid(myStr, cCtr, 1) = vbLf End If End If Next cCtr myCell.Value = myStr Next myCell End Sub
> Gord, > [quoted text clipped - 134 lines] > > >> > > > >> >Thanks in advance.
 Signature Dave Peterson
Saruman - 10 Jan 2008 00:10 GMT Thanks Dave,
That works a treat on some test data that I have at home here, will try on a copy of the original tomorrow. Will post back after then.
Thanks for your help!
Saruman
--------------------------------------------------------------------------- All Outgoing Mail Scanned By Symantec Antivirus 10 ---------------------------------------------------------------------------
> I _think_ that this is what you want. > [quoted text clipped - 182 lines] > > Dave Peterson Gord Dibben - 09 Jan 2008 21:55 GMT I understand but am having a hard time with my limited skills to locate the last digit in 505 where we would add a linefeed.
Also at last digit from 505
Gord
>Gord, > [quoted text clipped - 20 lines] > >If you are unable to assist, then thank you for your efforts. Saruman - 10 Jan 2008 00:11 GMT Gord,
Thanks for your help, Dave Peterson has a solution.
Many thanks for your time and assistance.
Saruman
--------------------------------------------------------------------------- All Outgoing Mail Scanned By Symantec Antivirus 10 ---------------------------------------------------------------------------
> I understand but am having a hard time with my limited skills to locate the last > digit in 505 where we would add a linefeed. [quoted text clipped - 27 lines] > > > >If you are unable to assist, then thank you for your efforts. Dave Peterson - 10 Jan 2008 00:33 GMT Just jumping in...
That's one of the good things about a collaborative effort. I read your posts and didn't understand them. Between Gord's questions and your clarifications, you may(!) have gotten something that works <bg>.
> Gord, > [quoted text clipped - 41 lines] > > > > > >If you are unable to assist, then thank you for your efforts.
 Signature Dave Peterson
Saruman - 10 Jan 2008 09:40 GMT Dave,
Code works. Many thanks to you and the other posters to my problem. This will save me hours of work.
Thanks again, post can now be closed.
Saruman
> Just jumping in... > [quoted text clipped - 56 lines] >> > > >> > >If you are unable to assist, then thank you for your efforts. Gord Dibben - 10 Jan 2008 00:47 GMT As I said.........one of the brighter lights........
Thanks from me also Dave.
I think I have maxed out......I can work it all out until you get down to the "For cCtr = Len(myStr) To 2 Step -1" part.
Then I start to mumble and fall over<g>
Back to fielding Tools>Options postings.
Gord
>Gord, > [quoted text clipped - 41 lines] >> > >> >If you are unable to assist, then thank you for your efforts. Dave Peterson - 10 Jan 2008 01:53 GMT How about this one--it might be quicker if there are lots of cells to be fixed:
Option Explicit Sub testme()
Dim myCell As Range Dim myRng As Range Dim cCtr As Long Set myRng = Nothing On Error Resume Next Set myRng = Intersect(Selection, _ Selection.Cells.SpecialCells(xlCellTypeConstants)) On Error GoTo 0 If myRng Is Nothing Then MsgBox "Please select a nice range" Exit Sub End If If myRng.Cells.Count < 2 Then Set myRng = Union(myRng, _ ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1)) End If myRng.Replace what:=vbLf, _ replacement:=Space(1), _ lookat:=xlPart, _ MatchCase:=False For Each myCell In myRng.Cells myCell.Value = Application.Trim(myCell.Value) Next myCell For cCtr = 0 To 9 myRng.Replace what:=cCtr & " ", _ replacement:=cCtr & vbLf, _ lookat:=xlPart, _ MatchCase:=False Next cCtr End Sub
It looks like every string that ends with a number (followed by a space) is converted to that number followed by alt-enter.
I couldn't think of a way to do an Edit|Replace to get rid of any leading or trailing spaces--so I still had to loop through each cell using application.trim().
> As I said.........one of the brighter lights........ > [quoted text clipped - 54 lines] > >> > > >> >If you are unable to assist, then thank you for your efforts.
 Signature Dave Peterson
CLR - 09 Jan 2008 17:07 GMT As you know, ASAP Utilities will get rid of all the extra spaces for you, then the regular Find&Replace will change them to CR's.......use Alt32 for the space, and Alt010 (both numbers from the KEYPAD) for the CR.......ReplaceAll
Vaya con Dios, Chuck, CABGx3
> I hope that someone can help me with a problem I have inherited with an > Excel 2003 file. [quoted text clipped - 24 lines] > > Thanks in advance. Saruman - 09 Jan 2008 19:16 GMT Chuck,
Thanks for the quick response. I can get ASAP Utilities to do the same as the result from Gordons Macro. It leaves a single item on each line which will then require attention to reformat them as a complete string on each line. There are 1800 lines with at least 2 complete line items in each cell which means a lot of manual work.
Maybe thats inevitable.
Thanks.
Saruman
--------------------------------------------------------------------------- All Outgoing Mail Scanned By Symantec Antivirus 10 ---------------------------------------------------------------------------
> As you know, ASAP Utilities will get rid of all the extra spaces for you, > then the regular Find&Replace will change them to CR's.......use Alt32 for [quoted text clipped - 32 lines] > > > > Thanks in advance.
|
|
|