我需要运行一个循环,将取出单元格的值,并将其存储在另一个,如果有什么,然后检查下一行

我有代码到目前为止我已经在下面张贴,但正如你可以看到它是非常不足的方法来做到这一点,并没有运行一个循环,不会帮助我,如果我结束不止A22以下的项目。 这里的代码是当我按下一个button时,它将通过工作簿中的每一张表单读取,如果单元格A5有“项目#:”,那么它会看到单元格A19是否有任何东西,如果它将采取什么单元格A19 ,C19,E19,F19和G19,并把它放在名为“数据表”的工作表上。 当前的代码有它,所以它通过A22处理,但我希望它是一个循环自动化,以便它检查单元格A5“项目#:”,如果它发现,然后它检查如果A19是空白的,如果它结束了循环。 如果它有任何types的文本,那么我希望它从列A,C,E,F和G的值,把它放在“数据表”。 然后它将循环到下一个(第20行),并重复它为前一个做的过程。 目前我的代码正确的工作,以获得数据的正确位置,但我的行19-22的IF-ELSE是效率低下,不知道如何编码循环。

Sub NonStoresItems() With Worksheets("Data Sheet") ' Clear previous data on the All projects page .Rows("141:" & Rows.Count).ClearContents For Each ws In ThisWorkbook.Worksheets If ws.Range("A5") = "Project # :" Then If Range("A19") = "" Then Else: x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$A$19" 'Non-stores material .Cells(x, "D").Formula = "='" & ws.Name & "'!$C$19" 'Lead Time .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$19" 'Order By Date .Cells(x, "G").Formula = "='" & ws.Name & "'!$F$19" 'Date Ordered .Cells(x, "H").Formula = "='" & ws.Name & "'!$G$19" 'Goals Met End If If Range("A20") = "" Then Else: x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$A$20" 'Non-stores material .Cells(x, "D").Formula = "='" & ws.Name & "'!$C$20" 'Lead Time .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$20" 'Order By Date .Cells(x, "G").Formula = "='" & ws.Name & "'!$F$20" 'Date Ordered .Cells(x, "H").Formula = "='" & ws.Name & "'!$G$20" 'Goals Met End If If Range("A21") = "" Then Else: x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$A$21" 'Non-stores material .Cells(x, "D").Formula = "='" & ws.Name & "'!$C$21" 'Lead Time .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$21" 'Order By Date .Cells(x, "G").Formula = "='" & ws.Name & "'!$F$21" 'Date Ordered .Cells(x, "H").Formula = "='" & ws.Name & "'!$G$21" 'Goals Met End If If Range("A22") = "" Then Else: x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$A$22" 'Non-stores material .Cells(x, "D").Formula = "='" & ws.Name & "'!$C$22" 'Lead Time .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$22" 'Order By Date .Cells(x, "G").Formula = "='" & ws.Name & "'!$F$22" 'Date Ordered .Cells(x, "H").Formula = "='" & ws.Name & "'!$G$22" 'Goals Met End If End If Next End With End Sub 

数据表输出 input

你可以用这个代替你的If...Else语句

 z = 19 Do While Not Range("A" & z) = "" x = .Range("A" & Rows.Count).End(xlUp).Offset(1).row .Cells(x, "A").Value = ws.Name 'classifying number .Cells(x, "B").Formula = "='" & ws.Name & "'!$A$" & z 'Non-stores material .Cells(x, "D").Formula = "='" & ws.Name & "'!$C$" & z 'Lead Time .Cells(x, "F").Formula = "='" & ws.Name & "'!$E$" & z 'Order By Date .Cells(x, "G").Formula = "='" & ws.Name & "'!$F$" & z 'Date Ordered .Cells(x, "H").Formula = "='" & ws.Name & "'!$G$" & z 'Goals Met z = z + 1 Loop 

这将循环,直到它findA列中的空值,如果你想在22每次停止只是添加AND z < 23Do While条件。