Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将范围转换为字符串(VBA)?_Vba_Macos_Excel_Excel 2011 - Fatal编程技术网

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

如何将范围转换为字符串(VBA)?,vba,macos,excel,excel-2011,Vba,Macos,Excel,Excel 2011,将一系列单元格转换为字符串的最佳方式是什么? 我有一个只接受字符串作为输入的函数,因此我需要将范围转换为字符串,同时保留尽可能多的格式(即,它需要看起来像一个表或列表,而不仅仅是一个字符串)。 我尝试过使用CStr(),以及从一个范围到一个数组再到一个字符串的转换,但我只是得到了错误 编辑:代码尝试 Dim email_answer As Integer email_answer = MsgBox("Do you want to be emailled a copy of this schedu

将一系列单元格转换为字符串的最佳方式是什么? 我有一个只接受字符串作为输入的函数,因此我需要将范围转换为字符串,同时保留尽可能多的格式(即,它需要看起来像一个表或列表,而不仅仅是一个字符串)。 我尝试过使用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
Dim email\u回答为整数
email_answer=MsgBox(“是否希望通过电子邮件向您发送此计划的副本?”,vbYesNo)
如果email\u answer=vbYes,则
将wb设置为工作簿
Dim to_发送为范围
to_send=范围(“D3”、“D10”)
如果Val(应用程序版本)<14,则退出Sub
设置wb=ActiveWorkbook
与wb
MailFromMacWithMail正文内容:=CStr(发送)_
mailsubject:=“日程安排”_
toaddress:=“电子邮件地址”_
ccaddress:=“”_
bccadAddress:=“”_
附件:=.全名_
displaymail:=False
以
设置wb=Nothing
如果结束

要创建一个以逗号分隔的单元格值列表,请执行以下操作:

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
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

您可以使用此功能:

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

    Dim myString as String
    myString = RangeToString(rng)
End Sub
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

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

这些函数中的任何一个都可以为您执行此操作

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


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

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

rng=左(rng,Len(rng))


将神奇地将rng转换为字符串。

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

考虑到迭代范围的明显需要,我认为首先将范围复制到数组中,然后通过循环数组来构建字符串要快得多。

即使在另一个工作表中有范围,也可以这样做

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

分享你的代码ATTMEPT你希望一个字符串看起来像一个表吗?抱歉@ShaiRado代码尝试现在添加(这是最后一次尝试,也尝试了范围->数组->字符串方法)。对于新手的问题,抱歉。我添加了您的代码,然后使用
Dim to_send As Range
to_send=Range(“D3”、“D10”)
调用RangeToString(to_send)
是否会更改为_send to a string?我的代码在执行此操作时仍然返回byref参数不匹配错误。我已编辑我的答案以向您展示如何使用此函数。该函数返回一个字符串对象,因此您可以将其分配给一个变量:)您太棒了,非常感谢!在移动到下一行之前,有没有办法让它沿着每一列运行?此时,它似乎在读取A1、B1、C1、A2、B2、C2等,而不是A1、A2、A3、B1、B2、B3等。您可以在myRange中的每个行范围的列
中循环。行
然后在每个
行范围
中的单元格中循环,而不是在整个
myRange
中循环。请注意,首先您应该将范围对象
Dim
设置为
myRow
谢谢,但现在它给了我一个运行时错误13类型不匹配。我是否正确实施了您的解决方案<代码>Dim myCell As Range Dim RowRange As Range myRange中每个RowRange的范围。RowRange RangeTString=RangeTString&Chr(13)&myCell中每个myCell的行。值Next myCell Next RowRange两次转置将2d数组转换为1d数组(这是我刚刚从中学到的)!@SMeaden,“行”范围需要它,而“列”范围只需要一个转置。它不需要…这里的输入是什么?你认为输出是什么?问题是转换一个范围的内容(在多个单元格上)对于字符串变量,不要将单个单元格的内容格式化为字符串…请解释您正在做什么以及为什么这是一个好方法