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

时间:2022-08-29 16:05:10

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 as much of the formatting as possible (i.e. it needs to look like a table or list, not just a string of characters). I've tried working with CStr(), as well as converting from a range to an array and then to a string, but I just get errors.

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

Edit: Code attempt

编辑:代码尝试

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

5 个解决方案

#1


6  

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

#2


5  

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

它将返回一个带换行符的字符串作为范围行分隔符和管道(“|”)作为列分隔符

#3


0  

Any one of these functions will do it for you.

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

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  

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.

我知道这个问题已经差不多一年了,但我找到了一个适合我的快速解决方案:你必须创建一个对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

#5


-1  

There is a much easier way. Assuming the variable rng is a range, then writing:

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

rng = Left(rng,Len(rng))

rng =左(rng,Len(rng))

will miraculously turn rng into a string.

将奇迹般地变成一根绳子。

#1


6  

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

#2


5  

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

它将返回一个带换行符的字符串作为范围行分隔符和管道(“|”)作为列分隔符

#3


0  

Any one of these functions will do it for you.

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

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  

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.

我知道这个问题已经差不多一年了,但我找到了一个适合我的快速解决方案:你必须创建一个对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

#5


-1  

There is a much easier way. Assuming the variable rng is a range, then writing:

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

rng = Left(rng,Len(rng))

rng =左(rng,Len(rng))

will miraculously turn rng into a string.

将奇迹般地变成一根绳子。