VBA excel 用剪贴板内容填充 Listobject

VBA excel populate Listobject with clipboard content

我正在尝试将剪贴板的内容(文本)插入到 listobject

代码:

   Dim myTbl As ListObject
   Set myTbl = ThisWorkbook.Sheets("applianswer").ListObjects(1)

    Dim obj As New DataObject
    obj.GetFromClipboard

    ' this is to be sure that the text is actually there
    MsgBox obj.GetText


    'this is to be sure that the databodyrange is actually existing. 2 as result
    MsgBox "nr of rows " & myTbl.ListRows.Count

    myTbl.DataBodyRange.ClearContents
    'this line gives error:
    myTbl.Paste Destination:=myTbl.ListColumns(1).DataBodyRange.iTem(1)

错误 我得到:

Run-time error 438 Object does not support this property or method

以下解决方案对我来说不是最优的:

myTbl.ListColumns("text").DataBodyRange.iTem(1).value = obj.GetText

因为这样所有内容都被插入到第一个单元格中 我希望文本沿着有换行符等的单元格分布

为什么粘贴不正确的一些想法? 谢谢。

粘贴到 table (ListObject) 中的问题总是针对您自己的具体情况。例如,如果我的剪贴板中有以下行:

row one for listobject
row two for listobject

我可能希望每一行都粘贴到 table 中它自己的行中,但我是希望第一个单元格中的整行还是剪贴板行中的每个单词分隔到 [= 中的不同列中? 33=]?如果剪贴板中有空行会怎样?不同的问题将需要修改您的解决方案以满足用户的期望。因此,虽然 Paste 可能对您有用(当然也可能作为手动操作工作),但如果您使用 VBA 执行操作,则可以更加具体。

对于下面的示例,我将清除并删除 table 中的所有行——除了一行。我发现在使用 table 时,如果 DataBodyRange.

中至少有一行,Excel 似乎会更快乐
'--- clear the table data and delete all the rows, because
'    the incoming data may have a different number of rows
With myTbl
    .DataBodyRange.ClearContents
    Do While .DataBodyRange.Rows.Count > 1
        .DataBodyRange.Rows(.DataBodyRange.Rows.Count).Delete
    Loop

接下来,设置一个对象,使我能够访问 table 中的其中一行。在本例中,它是 table.

中的唯一行
    Dim i As Long
    Dim thisRow As ListRow
    Set thisRow = .ListRows(1)

然后,我将从剪贴板中的文本创建一个行数组,沿 EOL 字符拆分:

    Dim lines() As String
    lines = Split(clipBoardText, vbCrLf)

现在只需遍历行并将剪贴板文本复制到 table 中的(新)行即可。

这是整个例程:

Option Explicit

Sub ClipboardToTable()
    Dim myTbl As ListObject
    Set myTbl = ThisWorkbook.Sheets("applianswer").ListObjects(1)

    Dim obj As DataObject
    Dim clipBoardText As String
    Set obj = New DataObject
    obj.GetFromClipboard
    clipBoardText = obj.GetText

    '--- clear the table data and delete all the rows, because
    '    the incoming data may have a different number of rows
    With myTbl
        .DataBodyRange.ClearContents
        Do While .DataBodyRange.Rows.Count > 1
            .DataBodyRange.Rows(.DataBodyRange.Rows.Count).Delete
        Loop

        Dim i As Long
        Dim thisRow As ListRow
        Set thisRow = .ListRows(1)

        Dim lines() As String
        lines = Split(clipBoardText, vbCrLf)
        For i = LBound(lines) To UBound(lines)
            If Len(lines(i)) > 0 Then
                '--- use this next statement if you want the entire
                '    line from the clipboard into the first cell on
                '    this table row
                thisRow.Range.Cells(1, 1).Value = lines(i)

                '--- use this next section if you want to split the
                '    clipboard line and distribute to the table columns\
                '    (example splits on the spaces)
                Dim j As Long
                Dim columnOffset As Long
                Dim parts() As String
                Dim numberOfParts As Long
                parts = Split(lines(i), " ")
                columnOffset = IIf(LBound(parts) = 0, 1, 0)
                numberOfParts = UBound(parts) + columnOffset
                '--- we might have to add new columns to fit the data
                Do While numberOfParts > .ListColumns.Count
                    .ListColumns.Add Position:=(.ListColumns.Count + 1)
                Loop
                For j = LBound(parts) To UBound(parts)
                    thisRow.Range.Cells(1, j + columnOffset).Value = parts(j)
                Next j

                Set thisRow = .ListRows.Add(AlwaysInsert:=True)
            End If
        Next i

        '--- delete the last listrow because it's empty from
        '    the loop above
        .ListRows(i).Delete
    End With
End Sub