Excel宏 - 将选择粘贴到Word中

时间:2011-03-31 07:32:26

标签: excel excel-vba ms-word copy-paste vba

我正在使用以下代码

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)

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

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