2016-07-25 55 views
0

有關我創建的背景,它是跟蹤項目的工作簿。現在我的工作簿中有3個項目,但是這個數字會超過50,並且會不斷增加。每個工作表包含項目信息Project info Worksheet。我有的代碼現在不工作。這張照片顯示了我想要的所有項目頁面的All projects page。我想要的代碼是通過每個工作表讀取的,如果$ A $ 5 =「Project#:」爲true,那麼它會將指定的單元格從該工作表複製到所有項目頁面。然後轉到下一張表格並執行相同的操作,如果它再次爲真,則將值放在最後一行的下一行。在每個工作表中循環查找數據並將其返回到特定工作表(如果有)

我猜我說的話是有點不清楚,這裏是我目前使用得到我想要的輸出,並在照片中對「所有項目」頁面的代碼:

Sub All_Data() 

'Sheet "1" 
Sheet7.Range("A2").Select 
ActiveCell.Formula = "=1!$F$1" 'classifying number 
Sheet7.Range("B2").Select 
ActiveCell.Formula = "=1!$B$5" 'Project # 
Sheet7.Range("C2").Select 
ActiveCell.Formula = "=1!$A$1" 'Project Name 
Sheet7.Range("D2").Select 
ActiveCell.Formula = "=1!$B$8" 'Project Engineer 
Sheet7.Range("E2").Select 
ActiveCell.Formula = "=1!$B$6" 'Maximo Time Charge 
Sheet7.Range("F2").Select 
ActiveCell.Formula = "=1!$E$5" 'Material Forecast due date 
Sheet7.Range("G2").Select 
ActiveCell.Formula = "=1!$E$11" 'Materials Forecast Actual 
Sheet7.Range("H2").Select 
ActiveCell.Formula = "=1!$F$11" 'Forecast success 
Sheet7.Range("I2").Select 
ActiveCell.Formula = "=1!$F$12" 'Non Stores Success 
Sheet7.Range("J2").Select 
ActiveCell.Formula = "=1!$E$6" '30% Due 
Sheet7.Range("K2").Select 
ActiveCell.Formula = "=1!$E$13" '30% actual 
Sheet7.Range("L2").Select 
ActiveCell.Formula = "=1!$F$13" '30% success 
Sheet7.Range("M2").Select 
ActiveCell.Formula = "=1!$E$7" '60% due 
Sheet7.Range("N2").Select 
ActiveCell.Formula = "=1!$E$14" '60% actual 
Sheet7.Range("O2").Select 
ActiveCell.Formula = "=1!$F$14" '60% Success 
Sheet7.Range("P2").Select 
ActiveCell.Formula = "=1!$E$8" '90% due 
Sheet7.Range("Q2").Select 
ActiveCell.Formula = "=1!$E$15" '90% actual 
Sheet7.Range("R2").Select 
ActiveCell.Formula = "=1!$F$15" '90% success 
Sheet7.Range("S2").Select 
ActiveCell.Formula = "=1!$B$11" 'In-service Due 
Sheet7.Range("T2").Select 
ActiveCell.Formula = "=1!$E$16" 'In-service actual 
Sheet7.Range("U2").Select 
ActiveCell.Formula = "=1!$F$16" 'In-service Success 


'Sheet "2" 
Sheet7.Range("A3").Select 
ActiveCell.Formula = "=2!$F$1" 'classifying number 
Sheet7.Range("B3").Select 
ActiveCell.Formula = "=2!$B$5" 'Project # 
Sheet7.Range("C3").Select 
ActiveCell.Formula = "=2!$A$1" 'Project Name 
Sheet7.Range("D3").Select 
ActiveCell.Formula = "=2!$B$8" 'Project Engineer 
Sheet7.Range("E3").Select 
ActiveCell.Formula = "=2!$B$6" 'Maximo Time Charge 
Sheet7.Range("F3").Select 
ActiveCell.Formula = "=2!$E$5" 'Material Forecast due date 
Sheet7.Range("G3").Select 
ActiveCell.Formula = "=2!$E$11" 'Materials Forecast Actual 
Sheet7.Range("H3").Select 
ActiveCell.Formula = "=2!$F$11" 'Forecast success 
Sheet7.Range("I3").Select 
ActiveCell.Formula = "=2!$F$12" 'Non Stores Success 
Sheet7.Range("J3").Select 
ActiveCell.Formula = "=2!$E$6" '30% Due 
Sheet7.Range("K3").Select 
ActiveCell.Formula = "=2!$E$13" '30% actual 
Sheet7.Range("L3").Select 
ActiveCell.Formula = "=2!$F$13" '30% success 
Sheet7.Range("M3").Select 
ActiveCell.Formula = "=2!$E$7" '60% due 
Sheet7.Range("N3").Select 
ActiveCell.Formula = "=2!$E$14" '60% actual 
Sheet7.Range("O3").Select 
ActiveCell.Formula = "=2!$F$14" '60% Success 
Sheet7.Range("P3").Select 
ActiveCell.Formula = "=2!$E$8" '90% due 
Sheet7.Range("Q3").Select 
ActiveCell.Formula = "=2!$E$15" '90% actual 
Sheet7.Range("R3").Select 
ActiveCell.Formula = "=2!$F$15" '90% success 
Sheet7.Range("S3").Select 
ActiveCell.Formula = "=2!$B$11" 'In-service Due 
Sheet7.Range("T3").Select 
ActiveCell.Formula = "=2!$E$16" 'In-service actual 
Sheet7.Range("U3").Select 
ActiveCell.Formula = "=2!$F$16" 'In-service Success 



'Sheet "3" 
Sheet7.Range("A4").Select 
ActiveCell.Formula = "=3!$F$1" 'classifying number 
Sheet7.Range("B4").Select 
ActiveCell.Formula = "=3!$B$5" 'Project # 
Sheet7.Range("C4").Select 
ActiveCell.Formula = "=3!$A$1" 'Project Name 
Sheet7.Range("D4").Select 
ActiveCell.Formula = "=3!$B$8" 'Project Engineer 
Sheet7.Range("E4").Select 
ActiveCell.Formula = "=3!$B$6" 'Maximo Time Charge 
Sheet7.Range("F4").Select 
ActiveCell.Formula = "=3!$E$5" 'Material Forecast due date 
Sheet7.Range("G4").Select 
ActiveCell.Formula = "=3!$E$11" 'Materials Forecast Actual 
Sheet7.Range("H4").Select 
ActiveCell.Formula = "=3!$F$11" 'Forecast success 
Sheet7.Range("I4").Select 
ActiveCell.Formula = "=3!$F$12" 'Non Stores Success 
Sheet7.Range("J4").Select 
ActiveCell.Formula = "=3!$E$6" '30% Due 
Sheet7.Range("K4").Select 
ActiveCell.Formula = "=3!$E$13" '30% actual 
Sheet7.Range("L4").Select 
ActiveCell.Formula = "=3!$F$13" '30% success 
Sheet7.Range("M4").Select 
ActiveCell.Formula = "=3!$E$7" '60% due 
Sheet7.Range("N4").Select 
ActiveCell.Formula = "=3!$E$14" '60% actual 
Sheet7.Range("O4").Select 
ActiveCell.Formula = "=3!$F$14" '60% Success 
Sheet7.Range("P4").Select 
ActiveCell.Formula = "=3!$E$8" '90% due 
Sheet7.Range("Q4").Select 
ActiveCell.Formula = "=3!$E$15" '90% actual 
Sheet7.Range("R4").Select 
ActiveCell.Formula = "=3!$F$15" '90% success 
Sheet7.Range("S4").Select 
ActiveCell.Formula = "=3!$B$11" 'In-service Due 
Sheet7.Range("T4").Select 
ActiveCell.Formula = "=3!$E$16" 'In-service actual 
Sheet7.Range("U4").Select 
ActiveCell.Formula = "=3!$F$16" 'In-service Success 

End Sub 

通過這段代碼,我可以將每個值分配到正確的位置,但是每個元素上的所有變化都是公式中的圖紙編號,並且行更改。

Sheet7.Range( 「U4」)。選擇 ActiveCell.Formula = 「= 3! $ F $ 16」「在業務成功

有沒有辦法,我可以寫某種循環將遍歷每張工作表,從「工程信息」工作表中獲取指定的數據並將其移至「所有工程」工作表,併爲每個新工作表分配一個新行。每個項目工作表在同一單元格位置包含相同的信息。

回答

2

這應該做的工作。

Sub allProjdata() 
    Dim ws As Worksheet 
    Dim x As Long 

    With Worksheets("Sheet1") 
     ' Clear previous data on the All projects page 
     .Rows("2:" & Rows.Count).ClearContents 

     For Each ws In ThisWorkbook.Worksheets 
      If ws.Range("A5") = "Project # :" Then 
       x = .Range("A" & Rows.Count).End(xlUp).Offset(1).Row 
       '.Cells(x, "A").Formula = "='" & ws.Name & "'!$F$1" 'classifying number 
       .Cells(x, "A").Value = ws.Name 

       .Cells(x, "B").Formula = "='" & ws.Name & "'!$B$5" 'Project # 
       .Cells(x, "C").Formula = "='" & ws.Name & "'!$A$1" 'Project Name 
       .Cells(x, "D").Formula = "='" & ws.Name & "'!$B$8" 'Project Engineer 
       .Cells(x, "E").Formula = "='" & ws.Name & "'!$B$6" 'Maximo Time Charge 
       .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$5" 'Material Forecast due date 
       .Cells(x, "G").Formula = "='" & ws.Name & "'!$E$11" 'Materials Forecast Actual 
       .Cells(x, "H").Formula = "='" & ws.Name & "'!$F$11" 'Forecast success 
       .Cells(x, "I").Formula = "='" & ws.Name & "'!$F$12" 'Non Stores Success 
       .Cells(x, "J").Formula = "='" & ws.Name & "'!$E$6" '30% Due 
       .Cells(x, "K").Formula = "='" & ws.Name & "'!$E$13" '30% actual 
       .Cells(x, "L").Formula = "='" & ws.Name & "'!$F$13" '30% success 
       .Cells(x, "M").Formula = "='" & ws.Name & "'!$E$7" '60% due 
       .Cells(x, "N").Formula = "='" & ws.Name & "'!$E$14" '60% actual 
       .Cells(x, "O").Formula = "='" & ws.Name & "'!$F$14" '60% Success 
       .Cells(x, "P").Formula = "='" & ws.Name & "'!$E$8" '90% due 
       .Cells(x, "Q").Formula = "='" & ws.Name & "'!$E$15" '90% actual 
       .Cells(x, "R").Formula = "='" & ws.Name & "'!$F$15" '90% success 
       .Cells(x, "S").Formula = "='" & ws.Name & "'!$B$11" 'In-service Due 
       .Cells(x, "T").Formula = "='" & ws.Name & "'!$E$16" 'In-service actual 
       .Cells(x, "U").Formula = "='" & ws.Name & "'!$F$16" 'In-service Success 
      End If 
     Next 

    End With 
End Sub 
+0

我調整我的代碼,並質疑一點點,你能重新閱讀我的問題和採樣代碼,看看,如果你看到一個辦法做到我的代碼更好,所以它會自動通過片去,我不必須複製並粘貼我的代碼並更改工作表和行。我爲上面的代碼做了這些工作,但我打算有超過50個項目,並且複製和粘貼,並且爲每行更改2個值需要一些嚴肅的時間 –

+0

基本上,您將每個「Project info Worksheet '到所有項目頁面上的行。 – 2016-07-26 00:52:59

+0

我更新了我的答案。讓我知道它是如何爲你工作的。 – 2016-07-26 01:22:51

相關問題