Excel VBA修剪功能

问题描述:

显然,修剪功能只修剪空格。我需要一个修剪所有非打印字符的函数。Excel VBA修剪功能

我的代码...

Private Sub CleanUpData() 
    LastRow = Application.CountA(ActiveSheet.Range("A:A")) 
    For CurrentRow = 2 To LastRow 
     Cells(CurrentRow, 1) = Trim(Cells(CurrentRow, 1)) 
     Cells(CurrentRow, 2) = Trim(Cells(CurrentRow, 2)) 
     Cells(CurrentRow, 3) = Trim(Cells(CurrentRow, 3)) 
     Cells(CurrentRow, 4) = Trim(Cells(CurrentRow, 4)) 
    Next CurrentRow 
End Sub 

...什么都不做。 !:(

帮助

试试这个:

Public Function TrimComplete(ByVal sValue As String) As _ 
     String 

     Dim sAns As String 
     Dim sWkg As String 
     Dim sChar As String 
     Dim lLen As Long 
     Dim lCtr As Long 

     sAns = sValue 
     lLen = Len(sValue) 

     If lLen > 0 Then 
      'Ltrim 
      For lCtr = 1 To lLen 
       sChar = Mid(sAns, lCtr, 1) 
       If (Asc(sChar) > 32) and (Asc(sChar) < 127) Then Exit For 
      Next 

      sAns = Mid(sAns, lCtr) 
      lLen = Len(sAns) 

      'Rtrim 
      If lLen > 0 Then 
       For lCtr = lLen To 1 Step -1 
        sChar = Mid(sAns, lCtr, 1) 
        If (Asc(sChar) > 32) and (Asc(sChar) < 127) Then Exit For 
       Next 
      End If 
      sAns = Left$(sAns, lCtr) 
     End If 

     TrimComplete = sAns 

    End Function 

Link to source

+0

嗯......采取无论你相信与否,它没有工作运行后!脚本,最后一些单元格仍然没有打印字符,这会让我发疯(是的,每个单元格都在处理中,我将它正确绑定) – BoltBait 2011-03-09 19:03:40

+0

OK,确定了它:非打印字符大于127.我将在上面编辑你的代码。 – BoltBait 2011-03-09 19:39:06