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

后端 未结 8 435
故里飘歌
故里飘歌 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:06

    I know this question is already almost a year old, but I found a quick solution that works for me: You do have to create a reference to Microsoft Forms 2.0 Object Library to use the DataObject.

    Public Function GetStringFromRange(RNG As Range) As String
        Dim DOB As New MSForms.DataObject
        RNG.Copy
        DOB.GetFromClipboard
        GetStringFromRange = DOB.GetText
    End Function
    
    0 讨论(0)
  • 2020-12-10 21:09

    This does the job, even with the Range in another Worksheet

    Function RangeToString(ByVal myRange As range) As String
        RangeToString = ""
        If Not myRange Is Nothing Then
            RangeToString = "=" & myRange.Worksheet.Name & "!" & myRange.Address
        End If
    End Function
    
    0 讨论(0)
  • 2020-12-10 21:11

    You can use the following solution to convert a range to a string in VBA:

    Sub convert()
    
    Dim rng As Range, cell As Range
    
    Dim filter As String
    
    filter = ""
    
    Set rng = Selection
    
    For Each cell In rng
    
        If Not cell Is Nothing Then
            filter = """" & cell & """" & "," & filter
        End If
    
    Next cell
    
    End Sub
    
    0 讨论(0)
  • 2020-12-10 21:11

    Given the apparent need to iterate the range, I'd imagine it'd be considerably quicker to copy the range to an array first, and build the string by looping the array.

    0 讨论(0)
  • 2020-12-10 21:12

    To make a comma separated list of cell values in a range:

    Function RangeToString(ByVal myRange as Range) as String
        RangeToString = ""
        If Not myRange Is Nothing Then
            Dim myCell as Range
            For Each myCell in myRange
                RangeToString = RangeToString & "," & myCell.Value
            Next myCell
            'Remove extra comma
            RangeToString = Right(RangeToString, Len(RangeToString) - 1)
        End If
    End Function
    

    You could add extra functionality like inserting a semicolon instead of a comma if the row number increases.

    To use this function:

    Sub AnySubNameHere()
        Dim rng As Range
        Set rng = ActiveSheet.Range("A3:A10")
    
        Dim myString as String
        myString = RangeToString(rng)
    End Sub
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题