2016-04-30 22 views
0

我希望這是一個正確的地方來問這個問題,因爲我正處於瘋狂的邊緣。我很生疏,我有VBA(只適用於C++,JAVA)從外部工作簿複製數據基於標題不是爲了另一個工作簿

問題零經驗: 我試圖將數據從一個工作簿複製到另一個。

可以說我有一個工作簿(稱爲數據)與幾個充滿數據的工作表。每一列數據都有一個唯一的標題(同一行上的所有標題)。

另一方面,我有另一個工作簿(稱爲報告)與一個工作表,只包含數據的標題(在一行)。它們與DATA工作簿中的順序不同。例如,我在REPORT工作表中可以在DATA工作簿的不同工作表中找到3個標題。

我需要遍歷DATA工作簿中的所有工作表,並在找到相同標題時將整列粘貼到REPORT工作表中。

此圖片可能有助於理解。Explanation

非常感謝ALOT提前給予的幫助。我已經搜索了很多代碼,但發現類似的東西,但沒有設法瞭解任何。

首先嚐試這樣做,但得到運行時錯誤「1004」的錯誤。 有什麼幫助嗎?

Dim MyFile As String 
Dim ws As Worksheet 

''Workbook that contains one worksheet with all the headings ONLY NO DATA 
Dim TargetWS As Worksheet 
Set TargetWS = ActiveSheet 
Dim TargetHeader As Range 

''Location of Headers I want to search for in source file 
Set TargetHeader = TargetWS.Range("A1:G") 

''Source workbook that contains multiple sheets with data and headings _ 
not in same order as target file 
Dim SourceWB As Workbook 
Set SourceWB = Workbooks("Source.xlsx") 
Dim SourceHeaderRow As Integer: SourceHeaderRow = 1 
Dim SourceCell As Range 

''Stores the col of the found value and the last row of data in that col 
Dim RealLastRow As Long 
Dim SourceCol As Integer 

''Looping through all worksheets in source file, looking for the heading I want _ 
then copying that whole column to the target file I have 
For Each ws In SourceWB.Sheets 
    ws.Activate 
    For Each Cell In TargetHeader 
    If Cell.Value <> "" Then 
      Set SourceCell = Rows(SourceHeaderRow).Find _ 
       (Cell.Value, LookIn:=xlValues, LookAt:=xlWhole) 
     If Not SourceCell Is Nothing Then 
       SourceCol = SourceCell.Column 
       RealLastRow = Columns(SourceCol).Find("*", LookIn:=xlValues, _ 
       SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 
       If RealLastRow > SourceHeaderRow Then 
       Range(Cells(SourceHeaderRow + 1, SourceCol), Cells(RealLastRow, _ 
        SourceCol)).Copy 
       TargetWS.Cells(2, Cell.Column).PasteSpecial xlPasteValues 
       End If 
     End If 
     End If 
    Next 
Next 

回答

0

你的問題沒有具體說明什麼問題的一部分,你實際上是停留在,所以我會假設你不知道如何下手。請注意,這裏沒有人會爲您提供完整的工作解決方案以解決您的問題 - 這取決於您自己。

的一些技巧,讓你開始工作:

  1. 你要問自己與涉及多個工作簿問題的第一個問題通常會是我該怎麼附上一份工作簿我的宏?

在你的情況下,報告工作簿看起來像一個理智的選擇,因爲你可能希望某人在該報告的東西,以便產生它被點擊。儘管如此,你也可以反過來辯論。

  1. 一旦您選擇了放置VBA的位置,您必須建立對其他工作簿的引用。

你要麼必須從磁盤加載使用Workbooks.Open其他Excel文件,或有兩個工作簿是在您的Excel實例,這我會建議你,因爲它更容易同時打開。在這種情況下,只需使用Workbooks對象建立參考。

Dim exampleRefToDATA As Workbook: Set exampleRefToDATA = Workbooks("data.xlsx") ' or index

  • 然後,通過每個工作表
  • 循環使用類似For Each ws As WorkSheet In exampleRefToDATA.WorkSheets作爲For循環

  • 在該循環中,使用類似於
  • 的文本循環遍歷第一列

    For Each allName As Range In ws.Range(... for you to figure out ...)

  • 在這個循環中,你必須看,如果該名稱是您REPORTS片做另一個循環像
  • For Each thisName As Range in Range(... seriously, there's enough on stackoverflow on how to properly iterate over the used range of a row ...)

    注如何撥打Range()等於ActiveWorkbook.ActiveWorkSheet.Range,這是您的報告表。

    1. 然後只需檢查相等性,並根據需要複製該行。同樣,複製一行也已在此處介紹過。

    希望這對你有幫助。

    +0

    謝謝你的時間,一個小問題。找到數據工作表中的匹配後,我正在查看..如何選擇此列並複製它? vba是否將記錄保存在停止搜索的位置? –

    +0

    你可以看看我在頂部使用的代碼嗎? –

    相關問題