关于我正在创建的内容的背景,它是一个跟踪项目的工作簿。现在我的工作簿中有3个项目,但这个数字将超过50个并且还在增长。每个工作表都包含项目信息Project info Worksheet。我的代码现在不起作用。这张图片展示了我希望它提供给All projects page这个所有项目页面的内容。我希望代码执行的是读取每个工作表,如果$ A $ 5 =“Project#:”为真,那么它会将指定的单元格从该工作表复制到所有项目页面。然后转到下一个工作表并执行相同操作,如果它再次为真,则将值放在最后一个下面的第1行。
我想我所说的有点不清楚,这里是我目前使用的代码,用于获取我想要的输出并在照片中的“所有项目”页面上显示:
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”'在职成功
有没有办法我可以编写某种循环来遍历每张工作表,从“项目信息”表中获取指定的数据并将其移动到“所有项目”表,并为每个新工作表分配一个新的行。每个项目表在相同的单元格位置包含相同的信息。
答案 0 :(得分: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