2011-03-31 137 views
0

我使用下面Excel宏 - 粘贴选择到Word

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)即可。

感谢您的帮助!

+0

如果有人正在寻找进一步的信息,只需看看这里:http://www.vbaexpress.com/kb/getarticle.php?kb_id=81 – JMax 2011-05-16 12:35:30

回答

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