gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:50:24 26 4
gpt4 key购买 nike

将一系列单元格转换为字符串的最佳方法是什么?
我有一个只接受字符串作为输入的函数,因此我需要将范围转换为字符串,同时保留尽可能多的格式(即它需要看起来像一个表格或列表,而不仅仅是一个字符串) .
我尝试过使用 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

最佳答案

要在一个范围内创建一个逗号分隔的单元格值列表:

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

关于vba - 如何将范围转换为字符串 (VBA)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41777996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com