如何将范围转换为字符串(VBA)?

时间:2017-01-21 10:02:24

标签: excel vba macos excel-vba excel-2011

将一系列单元格转换为字符串的最佳方法是什么? 我有一个函数只接受一个字符串作为输入所以我需要将范围转换为字符串,同时保留尽可能多的格式(即它需要看起来像一个表或​​列表,而不仅仅是一串字符) 。 我尝试过使用CStr(),以及从一个范围转换为一个数组然后转换为一个字符串,但我只是得到了错误。

编辑:代码尝试

Dim email_answer As Integer
email_answer = MsgBox("Do you want to be emailled a copy of this schedule?", vbYesNo)
If email_answer = vbYes Then

    Dim wb As Workbook
    Dim to_send As Range
    to_send = Range("D3", "D10")

    If Val(Application.Version) < 14 Then Exit Sub

    Set wb = ActiveWorkbook
    With wb
        MailFromMacWithMail body content:=CStr(to_send), _
                    mailsubject:="Schedule", _
                    toaddress:="email address", _
                    ccaddress:="", _
                    bccaddress:="", _
                    attachment:=.FullName, _
                    displaymail:=False
    End With
    Set wb = Nothing
End If

9 个答案:

答案 0 :(得分:6)

要在范围内以逗号分隔的单元格值列表:

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

如果行号增加,您可以添加额外的功能,例如插入分号而不是逗号。

使用此功能:

Sub AnySubNameHere()
    Dim rng As Range
    Set rng = ActiveSheet.Range("A3:A10")

    Dim myString as String
    myString = RangeToString(rng)
End Sub

答案 1 :(得分:5)

你可以使用这个功能:

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

将返回带换行符的字符串作为范围行分隔符和管道(&#34; |&#34;)作为列分隔符

答案 2 :(得分:1)

我知道这个问题已经差不多一年了,但我找到了一个适合我的快速解决方案: 您必须创建对Microsoft Forms 2.0对象库的引用才能使用DataObject。

Public Function GetStringFromRange(RNG As Range) As String
    Dim DOB As New MSForms.DataObject
    RNG.Copy
    DOB.GetFromClipboard
    GetStringFromRange = DOB.GetText
End Function

答案 3 :(得分:0)

这些功能中的任何一个都能为您完成。

Function ConCatRange2(CellBlock As Range) As String
'for non-contiguous cells =ccr((a1:a10,c4,c6,e1:e5))
Dim Cell As Range
Dim sbuf As String
For Each Cell In CellBlock
If Len(Cell.Text) > 0 Then sbuf = sbuf & Cell.Text & ","
Next
ConCatRange2 = Left(sbuf, Len(sbuf) - 1)
End Function

OR

Function mergem(r As Range) As String
mergem = r.Cells(1, 1).Value
k = 1
For Each rr In r
    If k <> 1 Then
        mergem = mergem & "," & rr.Value
    End If
    k = 2
Next
End Function

OR

Function spliceUm(r As Range) As String
spliceUm = ""
For Each rr In r
spliceUm = spliceUm & rr.Value & ";"
Next
End Function

答案 4 :(得分:0)

您可以使用以下解决方案在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

答案 5 :(得分:0)

鉴于明显需要迭代范围,我认为首先将范围复制到数组并通过循环数组来构建字符串要快得多。

答案 6 :(得分:0)

即使在另一个工作表中使用了范围,此操作也可以完成

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

答案 7 :(得分:0)

无需迭代。

Application.Textjoin(Chr(10),TRUE,Range("D3", "D10"))

Join(Application.Transpose(Range("D3:D10")),Chr(10))

答案 8 :(得分:-1)

有一种更简单的方法。 假设变量rng是一个范围,那么写:

rng =左(rng,Len(rng))

将奇迹般地变成一个字符串。