2011-03-09 130 views
0

顯然,修剪功能只修剪空格。我需要一個修剪所有非打印字符的函數。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 

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

幫助

回答

2

試試這個:

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