Excel 宏 - 将所选内容粘贴到 Word 中

发布于 2024-10-28 00:23:02 字数 695 浏览 7 评论 0原文

我正在使用下面的代码

iMaxRow = 200

" Loop through columns and rows"
For iCol = 1 To 3 
For iRow = 1 To iMaxRow

With Worksheets("GreatIdea").Cells(iRow, iCol)
    " Check that cell is not empty."
    If .Value = "" Then
        "Nothing in this cell."
        "Do nothing."
    Else
        " Copy the cell to the destination"
        .Copy Destination:=Worksheets("Sheet2").Cells(iRow, iCol)
    End If
End With

Next iRow
Next iCol

上面的代码仅适用于同一个 Excel 工作表。我想扩展它以便将其粘贴到Word文档中。我认为这是与;

appWD.Selection.PasteSpecial

但问题是,我还没有做出任何选择。代码很好,只是需要编辑 Copy Destination:=Worksheets("Sheet2").Cells(iRow, iCol)

感谢您的帮助!

I'm using the code below

iMaxRow = 200

" Loop through columns and rows"
For iCol = 1 To 3 
For iRow = 1 To iMaxRow

With Worksheets("GreatIdea").Cells(iRow, iCol)
    " Check that cell is not empty."
    If .Value = "" Then
        "Nothing in this cell."
        "Do nothing."
    Else
        " Copy the cell to the destination"
        .Copy Destination:=Worksheets("Sheet2").Cells(iRow, iCol)
    End If
End With

Next iRow
Next iCol

The code above is only applicable on the same excel sheet. I would like to extend it so it pastes into a word document. I presume it's something with;

appWD.Selection.PasteSpecial

But the problem is, I haven't made any selections. The code is good, just Copy Destination:=Worksheets("Sheet2").Cells(iRow, iCol) needs to be edited.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2024-11-04 00:23:02
iMaxRow = 200 ' or whatever the max is.
'Don't make too large because this will slow down your code.

' Loop through columns and rows
For iCol = 1 To 3 ' or however many columns you have
    For iRow = 1 To iMaxRow

    With Worksheets("GreatIdea").Cells(iRow, iCol)
        ' Check that cell is not empty.
        If .Value = "" Then
            'Nothing in this cell.
            'Do nothing.
        Else
            ' Copy the cell to the destination
            .Copy
            appWD.Selection.PasteSpecial
        End If
    End With

    Next iRow
Next iCol
iMaxRow = 200 ' or whatever the max is.
'Don't make too large because this will slow down your code.

' Loop through columns and rows
For iCol = 1 To 3 ' or however many columns you have
    For iRow = 1 To iMaxRow

    With Worksheets("GreatIdea").Cells(iRow, iCol)
        ' Check that cell is not empty.
        If .Value = "" Then
            'Nothing in this cell.
            'Do nothing.
        Else
            ' Copy the cell to the destination
            .Copy
            appWD.Selection.PasteSpecial
        End If
    End With

    Next iRow
Next iCol
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文