2011-03-31 77 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