请问excel宏中如何批量删除单元格中的空格和换行符?

2024-12-22 14:07:13
推荐回答(2个)
回答1:

只是去掉字符串两端的空格用trim函数就可以
换行符用replace 函数替换掉
Sub gs()
Dim arr
arr = Range("BE54:CB75").Value
For i = 1 To UBound(arr)
For j = 1 To UBound(arr, 2)
arr(i, j) = Trim(Replace(arr(i, j), Chr(10), ""))
Next
Next
Range("A1:B10") = arr
End Sub

回答2:

刚刚提问怎么不见了,回答道一半……
把下面这个部分分开来,写成两段
Find(" " & Chr(10), LookIn:=xlValues, lookat:=xlWhole)

——————————————————————————————————————

Set rng = ActiveSheet.Range("BE54:CB75").Find(" ", LookIn:=xlValues, lookat:=xlWhole)
If Not rng Is Nothing Then
a = rng.Address
Do
rng = ""
Set rng = ActiveSheet.Range("BE54:CB75").FindNext(after:=rng)
Loop While Not rng Is Nothing
End If

Set rng = ActiveSheet.Range("BE54:CB75").Find(Chr(10), LookIn:=xlValues, lookat:=xlWhole)

If Not rng Is Nothing Then
a = rng.Address
Do
rng = ""
Set rng = ActiveSheet.Range("BE54:CB75").FindNext(after:=rng)
Loop While Not rng Is Nothing
End If