2015-07-21 292 views
1

長時間閱讀器和StackOverflow的崇拜者。VBA - 從多個Excel文件複製並粘貼到單個Excel文件

基本上我試圖通過一系列Excel文件來複制一系列數據並將其粘貼到一個Excel工作簿/工作表上。

單元格區域位置(C3:D8,D3:E8)並不總是一致的,但表格尺寸爲:29 R x 2 C.此外,這些文件只有一個表格,並且除了指定的表格尺寸,其他單元格中沒有數據值。

在其當前形式的代碼正在執行,但沒有粘貼任何東西到其目標Excel文件。

我需要它

  1. 通過到下一個文件
  2. 查找文件(表)的數據維度
  3. 複製表
  4. 粘貼到目的地(以前見下表)
  5. 循環
  6. 重複步驟1-4

該代碼是從: Excel VBA: automating copying ranges from different workbooks into one final destination sheet?

非常感謝任何幫助,我真的很感激它,並請感覺告訴我,如果我的問題含糊不清,請指定任何內容。

Sub SourcetoDest() 

    Dim wbDest As Workbook 
    Dim wbSource As Workbook 
    Dim sDestPath As String 
    Dim sSourcePath As String 
    Dim shDest As Worksheet 
    Dim rDest As Range 
    Dim vaFiles As Variant 
    Dim i As Long 

    'array of folder names under sDestPath 

    'array of file names under vaFiles 
    vaFiles = Array("Book1.xls") 

    sDestPath = "C:\Users" 
    sSourcePath = "C:\Users" 


    Set wbDest = Workbooks.Open(sDestPath & "\" & "Book2.xlsm") 
    Set shDest = wbDest.Sheets(1) 

    'loop through the files 
    For i = LBound(vaFiles) To UBound(vaFiles) 
     'open the source 
     Set wbSource = Workbooks.Open(sSourcePath & "\" & vaFiles(i)) 

     'find the next cell in col C 
     Set rDest = shDest.Cells(shDest.Rows.Count, 3).End(xlUp).Offset(1, 0) 
     'write the values from source into destination 
     rDest.Resize(5, 1).Value = wbSource.Sheets(1).Range("C7:D33").Value 


     wbSource.Close False 
    Next i 

End Sub 
+0

你的代碼似乎很好,你有沒有嘗試在breakmode中通過它?你只需要這個部分來調整你的初始數據範圍,但是你不能在那裏處理任何東西(因爲你已經知道'End()'函數)。但是我不明白爲什麼在目標表中沒有任何數據... – R3uK

+0

如果您嘗試'wDSource.Sheets(1).Range(「C7:D33」)。請在rDest之前選擇。 Resize(5,1).Value = wbSource.Sheets(1).Range(「C7:D33」)。Value 'line,它會突出顯示源數據。使用F8瀏覽你的代碼並檢查你的源代碼範圍是否正確。接下來嘗試'rDest.Resize(5,1).Select'來檢查目標範圍。一旦這些是正確的,你可以在完成調試後刪除這兩行。 – tonester640

+0

謝謝,有趣的事情是當用F8滾動代碼時,它得到行設置wbDest = Workbooks.Open(sDestPath&「\」&「Book2.xlsm」)excel文件Book2打開,但隨後代碼停止? –

回答

1

下面的內容應該可以達到你想要的效果。

Option Explicit 
Sub copy_rng() 
    Dim wb As Workbook, wbDest As Workbook, ws As Worksheet, wsDest As Worksheet, wsSrc As Worksheet 
    Dim wbNames() As Variant 
    Dim destFirstCell As Range 
    Dim destColStart As Integer, destRowStart As Long, i As Byte 
    Dim destPath As String 

    Set wb = ThisWorkbook 
    Set ws = wb.Sheets("Sheet1") ' Amend to your sheet name 
    Set wsSrc = wb.Sheets("Sheet2") ' Amend to sheet name with table data 
    wbNames = ws.Range("A2:A" & lrow(1, ws)) ' Pass col number into lrow function 
    destPath = "C:\Users\" 

    Application.ScreenUpdating = False 
    For i = 1 To UBound(wbNames, 1) 
     Set wbDest = Workbooks.Open(destPath & wbNames(i, 1)) 
     Set wsDest = wbDest.Worksheets(1) 
     With wsDest 
      Set destFirstCell = .Cells.Find(What:="*") 
      destColStart = destFirstCell.Column 
      destRowStart = destFirstCell.Row 
      .Range(Cells(destRowStart, destColStart), _ 
       Cells(lrow(destColStart, wsDest), icol(destRowStart, wsDest))).Copy 
     End With 
     wsSrc.Cells(lrow(1, wsSrc) + 1, 1).PasteSpecial Paste:=xlPasteAll 
     wbDest.Close False 
    Next i 
    Application.ScreenUpdating = True 

End Sub 

Function lrow(ByVal col_num As Integer, sheet_name As Worksheet) As Long 
    lrow = sheet_name.Cells(Rows.Count, col_num).End(xlUp).Row 
End Function 

Function icol(ByVal row_num As Long, sheet_name As Worksheet) As Integer 
    icol = sheet_name.Cells(row_num, Columns.Count).End(xlToLeft).Column 
End Function 

確保您複製兩個函數,它們用於創建表的維度,然後複製表。

您將需要修改工作表名稱變量。如果您有任何問題,請告訴我。

您需要修改存儲工作簿名稱的範圍。您需要輸入列號,以便計算最後一行。您還可以修改將數據粘貼回工作簿的列。

+0

感謝回覆Iturner,我試圖複製並粘貼到最終文件中的數據維度的一件事是坐在單獨的excel文件中,是否可以修改WsSrc以使其轉到單獨的文件並從中提取數據? –

+0

上面創建一個包含工作簿名稱的數組(假定工作簿名稱存儲在A列中)。你在哪裏得到你想打開的工作簿名稱? –

+0

我看到了,感謝Iturner,之前我手動將工作手冊名稱放在VBA代碼中(數組格式),所以現在如果我將它們放在MyWorkBook中指示的工作表的A列中,它將自動通過這些工作簿。當我在我的電腦時,我會稍微嘗試一下:)再次感謝! –