簡體   English   中英

修剪 Function 不刪除空格 - VBA Excel

[英]Trim Function does not remove the spaces - VBA Excel

我已經看到了一些與這個問題相關的主題,不幸的是它們都沒有幫助。 修剪根本不會刪除之前或之后的空格......

宏應該通過列“F”廣告修剪所有邊緣,當前 id 通過列執行 go 並且在 MsgBox 的幫助下,我看到它正確獲取了單元格中的所有值,但實際修剪不起作用。

Sub trimAllTrends()

Dim i As Integer
Dim allInLastRow As Long
Dim allInWs As Worksheet
Dim myString As String

Set allInWs = ThisWorkbook.Worksheets("All Trends")
allInLastRow = allInWs.Range("C" & Rows.Count).End(xlUp).Row

For i = 2 To allInLastRow
    
    myString = allInWs.Cells(i, 6).Value
                'MsgBox (myString)
                'WorksheetFunction.Trim (allInWs.Cells(i, 6))
                'allInWs.Cells(i, 6).Value = LTrim(allInWs.Cells(i, 6).Value)
                '.Cells(i, "F").Value = Application.Trim(.Cells(i, "F").Value)
    WorksheetFunction.Trim (myString)
    
Next i


End Sub

任何幫助深表感謝!

先感謝您!

我不得不說我發現Trim() function 有點受限,尤其是從文字處理或頁面設計應用程序導入的數據。 所以大家在評論中提出的觀點都是好的。

如果你感興趣,我使用我自己的TrimWhitespace() function。 毫無疑問有更快的方法,但我發現這個適合我的目的:

Public Function TrimWhitespace(txt As String) As String
    Dim i As Long, j As Long, c As Long
    Dim startPos As Long, endPos As Long
    Dim whitespaces As Variant
    Dim isWhitespace As Boolean
    
    ' List of whitespace characters.
    whitespaces = Array( _
        &H9, &HA, &HB, &HC, &HD, &H20, &H85, &HA0, _
        &H1680, &H2000, &H2001, &H2002, &H2003, &H2004, &H2005, &H2006, _
        &H2007, &H2008, &H2009, &H200A, &H2028, &H2029, &H202F, &H205F, _
        &H3000, &H180E, &H200B, &H200C, &H200D, &H2060, &HFEFF)
    
    ' Find the first non-whitespace.
    For i = 1 To Len(txt)
        c = Asc(Mid(txt, i, 1))
        isWhitespace = False
        For j = LBound(whitespaces) To UBound(whitespaces)
            If c = whitespaces(j) Then
               isWhitespace = True
               Exit For
            End If
        Next
        If Not isWhitespace Then
            startPos = i
            Exit For
        End If
    Next
    
    ' If there's no start position, return an empty string.
    If startPos = 0 Then Exit Function
    
    ' Find the last non-whitespace.
    For i = Len(txt) To startPos Step -1
        c = Asc(Mid(txt, i, 1))
        isWhitespace = False
        For j = LBound(whitespaces) To UBound(whitespaces)
            If c = whitespaces(j) Then
               isWhitespace = True
               Exit For
            End If
        Next
        If Not isWhitespace Then
            endPos = i
            Exit For
        End If
    Next
    
    TrimWhitespace = Mid(txt, startPos, endPos - startPos + 1)
End Function

這是一些測試代碼來演示它:

Public Sub RunMe()
    Dim txt1 As String, txt2 As String
    
    txt1 = Chr(32) & Chr(160) & Chr(9) & "abc" & Chr(32) & Chr(160) & Chr(9)
    txt2 = Chr(32) & Chr(160) & Chr(9) & "xyz" & Chr(32) & Chr(160) & Chr(9)
    txt1 = Trim(txt1)
    txt2 = TrimWhitespace(txt2)
    
    Debug.Print "TRIM RESULTS"
    Debug.Print "============"
    Debug.Print "Trim()"
    Debug.Print "------"
    Debug.Print "Trimmed: |" & txt1 & "|"
    Debug.Print "Desired: |abc|"
    Debug.Print
    Debug.Print "TrimWhitespace()"
    Debug.Print "------------------------"
    Debug.Print "Trimmed: |" & txt2 & "|"
    Debug.Print "Desired: |xyz|"
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM