简体   繁体   English

根据单元格值从 Excel 工作表中提取数据

[英]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.您需要循环读取工作表中的数据,然后检查数据。

  1. this is the way to find last row content data这是查找最后一行内容数据的方法
lastRow = Cells(Rows.Count, 12).End(xlUp).Row  ' ,12 mean col L
  1. Then make a for loop to run, some thing like that然后做一个for循环来运行,类似这样的事情
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.

相关问题 使用 Excel 中的 VBA 将基于单元格值的工作表中的数据提取到新工作表中 - Extracting data from one sheet based on a cell value into a new sheet using VBA in Excel excel根据另一个单元格值从另一个工作表中提取数据 - excel pulling data from another sheet based on another cell value Excel如果单元格&lt;=值,如何将数据行从工作表1复制到工作表2 - Excel how copy row of data from sheet 1 to sheet 2 if a cell <= Value 根据列中的值填充其他工作表中的excel单元格值 - Populate excel cell value from a different sheet based on value in a column 根据单元格值将行从一个 Excel 工作表复制到另一个 - Copying rows from one Excel sheet to another based on cell value 根据公式中单元格中的值有条件地格式化 Excel 工作表选项卡 - Conditional Formatting an Excel Sheet Tab Based on Value in a Cell From a Formula 根据单元格值从另一张表中提取数据 - Pull data based from another sheet based on a cell value 根据第一张工作表中第二个单元格的值,将数据从一个单元格复制到另一张工作表中的单元格? - Copy data from one cell to a cell in another sheet based on the value of a second cell in the first sheet? 根据范围内单元格的值隐藏Excel工作表 - Hide Excel sheet based on value of a cell in a range 将数据从Siebel数据库提取到Excel工作表 - Extracting Data from Siebel Database to Excel Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM