How can I convert a range to a string (VBA)?

后端 未结 8 453
故里飘歌
故里飘歌 2020-12-10 20:38

What is the best way to convert a range of cells to a string? I have a function that only takes a string as input so I need to convert the range to a string, while retaining

8条回答
  •  甜味超标
    2020-12-10 21:21

    you could use this function:

    Function Rang2String(rng As Range) As String
        Dim strng As String
        Dim myRow As Range
        With rng
            For Each myRow In .Rows
                strng = strng & Join(Application.Transpose(Application.Transpose(myRow.value)), "|") & vbLf
            Next
        End With
        Rang2String = Left(strng, Len(strng) - 1)
    End Function
    

    which would return a string with linefeed character as range rows separator and pipes ("|") as columns separator

提交回复
热议问题