当然,有一些方法可以从一开始就避免这种模式,但这是一个宏来进行(后期)清理:
Sub Cleanup()
Dim cel As Range, i As Long
With Worksheets("Products").UsedRange.Columns("J")
For i = 1 To 5
.Replace "- - ", "- "
Next
For Each cel In .Cells
If Trim(cel.Value) = "-" Then cel.Clear
Next
End With
End Sub
编辑:
既然你我都没有TextJoin,而且是小伙伴们提出的一个很好的解决方案,就让我们把它当作UDF吧。您可以将以下代码添加到任何非类代码模块并将其用作用户定义公式 (UDF):
Public Function TextJoin(ByVal sep As String, ByVal ignoreEmpty As Boolean, ByRef ar As Variant) As String
Dim cel As Range
For Each cel In ar
If Trim(cel.Text) <> "" Or Not ignoreEmpty Then
If TextJoin <> "" Then TextJoin = TextJoin + sep
TextJoin = TextJoin + cel.Text
End If
Next
End Function