[英]Extracting data from an Excel sheet based on cell value
I have a workbook tracking parts being produced including vendors, quantities and delivery dates etc. It is a big file, and I regularly have to manually pull specific parts out to create reports.我有一个工作簿跟踪正在生产的零件,包括供应商、数量和交货日期等。这是一个大文件,我经常需要手动拉出特定零件来创建报告。
What I'm trying to do with VBA:我想用 VBA 做什么:
-Test each row of a specific sheet for an overdue shipment based on specified delivery date - 根据指定的交货日期测试特定工作表的每一行是否有逾期发货
-If the part is overdue, copy the entire row into a new workbook - 如果部分逾期,则将整行复制到新工作簿中
-Continue the process row by row until it reaches the end - 逐行继续处理,直到到达终点
-Pull out every part with all its details that is overdue into a new sheet - 将过期的每个部分及其所有细节拉出到新表中
What I got so far, for a test workbook:到目前为止,对于测试工作簿,我得到了什么:
Sub btnPrintReport_click()
Dim part As String, vendor As String, date_due As String
part = Range("L3:L10").Select
vendor = Range("M3:M10").Select
date_due = Range("N3:N10").Select
If date_due < Date Then
Range("A21").Value = part
Range("B21").Value = vendor
Range("C21").Value = date_due
End If
End Sub
The best I could get is to read my test book and print the first values from L, M, N but then it stops because I cannot get it to continue the process for all other rows (it reads row 3, and stops).我能得到的最好的方法是阅读我的测试书并打印 L、M、N 中的第一个值,但随后它停止了,因为我无法让它继续处理所有其他行(它读取第 3 行,然后停止)。
I figured a loop would fix that but I'm struggling to get it to work.我想一个循环可以解决这个问题,但我正在努力让它工作。
you need for loop to read data in your worksheet then check data.您需要循环读取工作表中的数据,然后检查数据。
lastRow = Cells(Rows.Count, 12).End(xlUp).Row ' ,12 mean col L
for i = 3 to lastRow
'put your check and copy process here
' for example
part = cells(i,12) ' i = 3 then its mean cells(3,12) = L3 then so on
.
.
.
If date_due < Date Then
cells(Cells(Rows.Count, 1).End(xlUp).Row,1).value = part
cells(Cells(Rows.Count, 2).End(xlUp).Row,2).value = vendor
cells(Cells(Rows.Count, 3).End(xlUp).Row,3).value = date_due
End If
next i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.