2014-04-28 70 views
0

我将这些数据放在一个单元格中。如何用单行换行替换多行换行

This 
is 



a 




bad dream 

对于我的报告格式的目的,我需要减少多个换行。只要2个字符串之间有2个以上的换行符,就需要换行2个换行符。所以数据必须看起来像

This 
is 

a 

bad dream 

我只是一个在excel脚本初学者。我尝试使用替代函数,但无法获得正确的结果。

一些循环可以使用替代?

回答

3

只要2个字符串之间有超过2个换行符,就需要用2个换行符来替换它们。

使用这个小功能:

Function customSubstitute(myStr As String) As String 
    customSubstitute = myStr 
    Do While InStr(1, customSubstitute, Chr(10) & Chr(10) & Chr(10)) 
     customSubstitute = Replace(customSubstitute, Chr(10) & Chr(10) & Chr(10), Chr(10) & Chr(10)) 
    Loop 
End Function 
+0

感谢simoco。这工作得很好。我使用了下面的代码。我想,需要一些额外的编码。 Sub removeempty() Dim str As String,n As Long,a As Long Dim line2 As String,line3 As String str = Range(“A1”)。Value a = Len(str) line2 = Chr(10 )&Chr(10) 如果Mid(str,n,3)= line3然后 str = Application.Substitute(10)&Chr(10)&Chr(10) 对于n = 1到 STR,MID(STR,N,3),2号线) MSGBOX STR 结束如果 然后,下次 范围( “C1”)。值= STR 结束小组 – user3582124

+0

编辑与此代码的问题,因为它是在评论不可读 –