首页 > 解决方案 > 有没有办法将多个数据结果返回到一个单元格中?

问题描述

我正在构建一个代码来从多个文档中检索所有文本,然后将该文档的文件名放在 A 列中,然后将该文档中包含的所有文本放在同一行中,但在 B 列中。现在,我有代码到我可以从这些文档中检索文本的位置,但我的问题是我无法将所有文本条目合并到一个单元格中。每个文档包含不同数量的文本条目,因此代码必须将所有文本包含在一个单元格中。

这是我到目前为止所拥有的:

    Dim ent As AXDBLib.AcadEntity
    Dim txt As AcadText

Dim mtxt As AcadMText

Dim textValue As String

                      For Each ent In ActiveDocument.ModelSpace

                            If TypeOf ent Is AcadText Then

                                Set txt = ent
                                
                                Cells(4, 2) = txt.TextString
                                Debug.Print txt.TextString

                            End If

标签: excelvbaautocad

解决方案


请尝试下一个方法:

Sub testAllInACell()
 'your existing code...
    For Each ent In ActiveDocument.ModelSpace
            If TypeOf ent Is AcadText Then
                Set txt = ent
                 If textvalue = "" Then
                    textvalue = txt.TextString
                 Else
                    textvalue = textvalue & vbLf & txt.TextString
                 End If
            End If
    Next
    If Len(textvalue) <= 32767 Then
            Cell(4, 2).value = textvalue
    Else
            MsgBox "The text to be copied exceeds the maximum allowed... " & vbCrLf & _
                   "(" & Len(textvalue) & " against 32767, admitted)"
    End If
    'Your existing code (if any...)
End Sub

推荐阅读