2011-03-31 71 views
1

所以我想通過A1-C200運行,並將所有內容粘貼到Word文檔中。麻煩的是,我有兩種將它粘貼到Word中的方法,但每一種都有它的缺陷。Excel宏 - 通過同一級別上的單元格運行

目標:將A1-C200複製到Word中並保留列布局,而不復制空白。

實施例1:

下面拷貝到一切Word中的代碼,但是從A1運行 - > A200,B1 - > B200,C1 - > C200。因爲它以這種方式讀取我的文件,所以我失去了我的列布局。我更喜歡這個例子的解決方案,因爲這個代碼看起來更清晰。

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 
      appWD.Selection.PasteSpecial 
     End If 
    End With 

    Next iRow 
Next iCol 

實施例2:

下面副本的代碼正確的列的佈局,但也可以插入BLANCS。所以如果填寫A1-A5和A80-A90,我的Word文檔中就會有75個空白。

a1 = Range("A1").End(xlDown).Address 
lastcell = Range("C1").Address 
Range(a1, lastcell).Copy 
With Range("A1") 
Range(.Cells(1, 1), .End(xlDown).Cells(2, 3)).Copy 
End With 
Range("A1:C50").Copy 
appWD.Selection.PasteSpecial 
+0

這個空白應該會發生什麼?您想要將A80-A90的內容在A1-A5旁邊移動嗎?或者將這些值保存在Word表格中,以免它們被空白覆蓋?假設它是一個Word表格?請澄清。 – 2011-03-31 18:29:32

+0

另外你的第二個代碼示例是一個真正的混亂!前7行是相互冗餘的;你正在複製3個不同的範圍,而不是粘貼到任何地方。 – 2011-03-31 18:30:48

+0

哦,例2是我的第一個編碼。但是,是的,我希望A80-A90的內容向上移動,以便它們進入A1-A5。 – CustomX 2011-04-01 07:26:14

回答

0

有多種方法可以做到這一點,不知道哪個是最快的,但這裏的一些代碼,我扔在一起真正快給你的。在變體中同時獲取範圍是從Excel中抓取數據的最快方法。

Sub test() 

     Dim i As Long, j As Long 
     Dim wd As Word.Document 
     Dim wdTable As Word.Table 
     Dim wks As Excel.Worksheet 
     Dim v1 As Variant 
     Set wd = GetObject("C:\Documents and Settings\Jon\Desktop\New Microsoft Word Document.doc") 

'Get data in array 
     Set wks = ActiveSheet 
     v1 = wks.UsedRange   

'Create table 
     Set wdTable = wd.Tables.Add(Range:=wd.Application.Selection.Range, NumRows:=1, NumColumns:= _ 
      ubound(v1,2), DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _ 
      wdAutoFitFixed) 


     'Place data 
     For i = 1 To UBound(v1) 
      For j = 1 To UBound(v1, 2) 
       If Len(v1(i, j)) > 0 Then 
        'Add row if not enough rows, this can be done before the j loop if 
        'you know the first column is always filled. 
        'You can also do an advanced filter in excel if you know that the first 
        'column is filled always and filter for filled cells then just 
        'do a straight copy and paste using r1.specialcells(xlCellTypeVisible).copy 
        'If you know the rows ahead of time when you create the table you can create all the rows at once, 
        'which should save time. 
        wd.application.selection 
        If wdTable.Rows.Count < i Then wdTable.Rows.Add 
        wdTable.Cell(i, j).Range.Text = v1(i, j) 
       End If 
      Next j 
     Next i 

     Set wks = Nothing: Set wd = Nothing: Set v1 = Nothing 
    End Sub 
+0

感謝您的評論,但這給了application.selection一個錯誤。 – CustomX 2011-04-01 07:39:38

+0

你剛剛複製並粘貼我的代碼或與你的代碼一起使用?如果你將它與你的一起使用,你可能會把wd當作word.application,在這種情況下,你不需要wd.application.selection,你只需要wd.selection。 – Jon49 2011-04-01 18:40:06

+0

我複製了你的整個代碼,它仍然給出錯誤。但我認爲這是因爲我不選擇或複製任何東西?我認爲你的評論在哪裏,我需要添加一些選擇或複製? – CustomX 2011-04-04 10:09:32

0

不能肯定我理解的概率......但這裏是它刺傷:

dim rg200x3 as range: set rg200x3 = range("a1:c200") 

dim Col1 as new collection 
dim Col2 as new collection 
dim Col3 as new collection 

dim rgRow as new range 
dim sText as string 
for each rgRow in rg200x3 
    sText = trim(rgRow.cells(1,1)): if (sText <> "") call Col1.Add(sText) 
    sText = trim(rgRow.cells(1,2)): if (sText <> "") call Col2.Add(sText) 
    sText = trim(rgRow.cells(1,3)): if (sText <> "") call Col3.Add(sText) 
next rgRow 

在這一點上Col1中,col2的,和COL3包含文本W¯¯分解出來的空白單元格,所以現在遍歷這些打印出來

dim i as long 
for i = 1 to 200 
    on error resume next ' (cheap way to avoid checking if index > collection sz) 
    debug.print Col1(i) + " | " Col2(i) + " | " + Col3(i) 
    on error goto 0 
next i 

(注:在寫意類型沒有檢查代碼...)

+0

但是,如何將其複製到Word中? – CustomX 2011-04-01 07:11:44

0

如何這個給你的第一個解決方案子:

iMaxRow = 200 

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

    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 iCol 
Next iRow 
+0

你所做的只是把appWD放在後面。複製大聲笑? :P – CustomX 2011-04-01 07:28:23

+0

@Tom,沒有那只是格式化。我所做的只是將Row設置爲外部循環,將Column設置爲內部循環。 – 2011-04-01 15:08:28

+0

哦好吧:)謝謝 – CustomX 2011-04-01 15:15:59

相關問題