Excel VBA TRIM 宏不修剪电子邮件地址

Excel VBA TRIM macro not trimming email addresses

我有一个宏 trim 用于当前选定列中的所有单元格,当我在普通文本、名称、邮政编码等上使用它时它工作得很好,但是当它用于 trim 一个电子邮件地址列,它没有删除尾随空格。

'这需要trimming'变成'This needs trimming'但是
'thisneeds@trimming.to ' 保持为 'thisneeds@trimming.to '。

这是宏,

Dim mycell
mycell = ActiveCell.Address

ActiveCell.EntireColumn.Select

    response = MsgBox("Are you sure you want to trim column " & Split(mycell, "$")(1) & "?", vbYesNo)

If response = vbNo Then
    Exit Sub
End If

Dim arrData() As Variant
Dim arrReturnData() As Variant
Dim rng As Excel.Range
Dim lRows As Long
Dim lCols As Long
Dim i As Long, j As Long

  lRows = Selection.Rows.Count
  lCols = Selection.Columns.Count

  ReDim arrData(1 To lRows, 1 To lCols)
  ReDim arrReturnData(1 To lRows, 1 To lCols)

  Set rng = Selection
  arrData = rng.Value

  For j = 1 To lCols
    For i = 1 To lRows
      arrReturnData(i, j) = Trim(arrData(i, j))
    Next i
  Next j

  rng.Value = arrReturnData

  Set rng = Nothing

Range(mycell).Select

我不明白为什么它适用于每一列但不适用于电子邮件地址。

不用重新发明轮子也能过得去。

请使用此函数作为示例代码继续。

Public Function TrimStr(inputStr As String) As String
    TrimStr = WorksheetFunction.Trim(inputStr)
End Function

我已经用你的例子和其他例子试过了,各种空格都被删除了。