这是我试图通过工作表中包含字符串的所有单元格实现的目标,但到目前为止取得的成功有限:
|示例 |
cell1_empty_line
cell1_text1
cell1_empty_line
+--------------------------------+
cell2_text1
cell2_emptyline
cell2_text2
+--------------------------------+
cell3_emptyline
cell3_emptyline
cell3_text1
+--------------------+
|预期结果 |
cell1_text1
+--------------------------------+
cell2_text1
cell2_text2
+--------------------------------+
cell3_text1
+--------------------+
对于这样的宏有什么建议吗?
非常感谢。
最佳答案
使用此宏删除所有单元格内的任何空行:
Sub TrimEmptyLines()
Dim cel As Range, s As String, len1 As Long, len2 As Long
For Each cel In ActiveSheet.UsedRange
If Not IsError(cel.Value2) Then
If InStr(1, cel.text, vbLf) > 0 Then
s = Trim(cel.Value2)
Do ' remove duplicate vbLf
len1 = Len(s)
s = Replace$(s, vbLf & vbLf, vbLf)
len2 = Len(s)
Loop Until len2 = len1
' remove vblf at beginning or at end
If Left$(s, 1) = vbLf Then s = Right$(s, Len(s) - 1)
If Right$(s, 1) = vbLf Then s = Left$(s, Len(s) - 1)
cel.value = Trim$(s)
End If
End If
Next
End Sub
关于vba - 如何从 Excel 中的单元格中删除空行(VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841374/