View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default How to delete commas and spaces when not needed ... S&R way, or macro way?

How about a macro? Just change the two Const (constant) assignments to the
column letter and sheet name where your data is located at...

Sub CleanUpCommaSpaces()
Dim X As Long
Dim R As Range
Dim LastRow As Long
Const ColumnLetter As String = "A"
Const SheetName As String = "Sheet6"
With Worksheets(SheetName)
LastRow = .Columns(ColumnLetter).SpecialCells(xlCellTypeLast Cell).Row
For X = 1 To LastRow
With .Cells(X, ColumnLetter)
If .Value Like ", *" Then
.Value = Mid(.Value, 3)
ElseIf .Value Like "*, " Then
.Value = Left(.Value, Len(.Value) - 2)
End If
End With
Next
End With
End Sub


Oh, just noticed, will this do the whole workbook, or just sheetname
"Sheet6", since all our sheets have specific labels. But what a good
idea if the macro could handle the entire workbook at once!

Also, does Const ColumnLetter As String = "A" mean that only column A
will be handled? That would be great because I could then choose the
actual real columns to fix each time, in this case, each worksheet has
2 columns.


Okay, this macro will process each sheet in the workbook and, yes, it only
does one column (the column letter that you assign to the ColumnLetter
constant... it does this same column on each sheet). And to clarify, it only
removes the comma-space (both characters must be present) from the beginning
or the end of the text in a cell and no where else... and to further
clarify, it only does this for cells contain fixed text (that is, cells
whose value does NOT come from a formula)...

Sub CleanUpCommaSpaces()
Dim X As Long
Dim R As Range
Dim LastRow As Long
Dim WS As Worksheet
Const ColumnLetter As String = "A"
On Error GoTo Whoops
Application.ScreenUpdating = False
For Each WS In Worksheets
LastRow = WS.Columns(ColumnLetter).SpecialCells(xlCellTypeLa stCell).Row
For X = 1 To LastRow
With WS.Cells(X, ColumnLetter)
If Not .HasFormula Then
If .Value Like ", *" Then
.Value = Mid(.Value, 3)
ElseIf .Value Like "*, " Then
.Value = Left(.Value, Len(.Value) - 2)
End If
End If
End With
Next
Next
Whoops:
Application.ScreenUpdating = True
End Sub

--
Rick (MVP - Excel)