文字列から空白行を削除

' 機能概要: 文字列から空白行を削除
' 引数:
'   strText - 処理する文字列
' 戻り値:
'   String - 空白行が削除された文字列

Function RemoveBlankLines(strText As String) As String
    Dim arrLines As Variant
    Dim strResult As String
    Dim i As Integer

    ' 文字列を行に分割
    arrLines = Split(strText, vbCrLf)

    ' 各行をチェックし、空白でない行を結合
    strResult = ""
    For i = LBound(arrLines) To UBound(arrLines)
        If Trim(arrLines(i)) <> "" Then
            strResult = strResult & arrLines(i) & vbCrLf
        End If
    Next i

    ' 最後の改行を削除
    If Right(strResult, 2) = vbCrLf Then
        strResult = Left(strResult, Len(strResult) - 2)
    End If

    RemoveBlankLines = strResult
End Function

コメント