将列中的元素转换为vba excel中的单个文本

时间:2014-07-23 07:20:18

标签: excel vba

假设中间有空白列,中间可以有多个空白列, 我如何在vba excel中进行编码。

列中的项目:

Nice
to
meet
you.

因此我正在寻找:

Nice to meet you.

1 个答案:

答案 0 :(得分:2)

如评论所述,这是我为了一次性连接Range值而编写的函数 它根据您在单元格中的显示方式将其连接起来 我不完全确定,如果我已经涵盖了所有可能性,但你可以尝试一下。

Public Function CONCATPLUS(ref_value As Range, Optional delimiter As Variant) As String
    Dim cel As Range
    Dim refFormat As String, myvalue As String

    If ref_value.Cells.Count = 1 Then CONCATPLUS = CVErr(xlErrNA): Exit Function
    If IsMissing(delimiter) Then delimiter = " "

    For Each cel In ref_value
        refFormat = cel.NumberFormat
        Select Case TypeName(cel.Value)
        Case "Empty": myvalue = vbNullString
        Case "Date": myvalue = Format(cel, refFormat)
        Case "Double"
            If refFormat <> "General" Then
                myvalue = Format(cel, refFormat)
            Else
                myvalue = cel
            End If
        Case "Error"
            Select Case True
            Case cel = CVErr(xlErrDiv0): myvalue = "#DIV/0!"
            Case cel = CVErr(xlErrNA): myvalue = "#N/A"
            Case cel = CVErr(xlErrName): myvalue = "#NAME?"
            Case cel = CVErr(xlErrNull): myvalue = "#NULL!"
            Case cel = CVErr(xlErrNum): myvalue = "#NUM!"
            Case cel = CVErr(xlErrRef): myvalue = "#REF!"
            Case cel = CVErr(xlErrValue): myvalue = "#VALUE!"
            Case Else: myvalue = "#Error"
            End Select
        Case Else: myvalue = cel
        End Select

        If Len(myvalue) <> 0 Then
            If CONCATPLUS = "" Then
                CONCATPLUS = myvalue
            Else
                CONCATPLUS = CONCATPLUS & delimiter & myvalue
            End If
        End If
    Next
End Function