Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clean Function Multiple Columns

Status
Not open for further replies.

NYFashionToGo

Technical User
Jan 16, 2007
76
US
I have been trying to figure this out, I have 2 clean fuctions One cleans The whole sheet. and The second cleans Just once single column. How would I alter this to clean a few columns (Not all) But not one:


Cleans All Cells
Sub cleanup1()
Dim TheCell As Range

For Each TheCell In ActiveSheet.UsedRange
With TheCell
If .HasFormula = False Then
.Value = Application.WorksheetFunction.Clean(.Value)
End If
End With
Next TheCell
End Sub



Cleans One Column (Column C)

Sub cleanup()
Dim TheCell As Range
'The Number 3in the next row is the column number.
For Each TheCell In ActiveSheet.UsedRange.Columns(3).Cells

With TheCell
If .HasFormula = False Then
.Value = Application.WorksheetFunction.Clean(.Value)
End If
End With
Next TheCell

End Sub

I tried Adding (3),(4),(5) a number of different ways and it did not work. I dont know what I am doing wrong..

Can anyone assist? Thanks

 
How about:
Code:
Sub cleanup([b]intRow[/b])
Dim TheCell As Range
'The Number 3in the next row is the column number.
For Each TheCell In ActiveSheet.UsedRange.Columns([b]intRow[/b]).Cells
    With TheCell
    If .HasFormula = False Then
        .Value = Application.WorksheetFunction.Clean(.Value)
    End If
    End With
Next TheCell

End Sub

But you would need to pass which column you need to clean:
Call cleanup(4)



Have fun.

---- Andy
 



Hi,

Pass a From, Thru ColNbr
Code:
Sub cleanup(nFrom as integer, nThru as integer)
Dim iCol as integer
'The Number 3in the next row is the column number.
For icol = nfrom to nthru

    With With Cells(activesheet.usedrange.row,iCol)
      If .HasFormula = False Then
        .Value = Application.WorksheetFunction.Clean(.Value)
      End If
    End With
Next 

End Sub

Skip,

[glasses] [red][/red]
[tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top