当特定列的答案为YES时,如何让Excel VBA在字段中提取特定数据

时间:2011-06-08 13:15:27

标签: excel-vba vba excel

我正在使用Excel 2003,我试图从工作表中提取特定数据。我想要的是当在具有特定名称的列下填充“是”时,如果答案为是,则它将遍历整行数据。这是我创建的一些VBA代码。我有工作表库存有所有数据,希望数据可以通过报告工作表。任何帮助都会得到很好的接受。

Sub AnalyseEMPHInfo()
Dim rngReports As Range
Dim rngInventory As Range
Dim StrSchemeNumber As String
Dim StrUnitName As String
Dim StrTCsSigned As String
Dim strVetSurveyCompleted As String
Dim StrBiosecurityReceived As String
Dim StrCDSurveyReceived As String
Dim strBPHSOptInOut As String

Set rngReports = Worksheets("Reports").Range("A2")
Set rngInventory = Worksheets("Inventory").Range("A2")
Do Until rngInventory = ""
    With rngInventory
        If .Offset(0, 1).Value = StrSchemeNumber And .Offset(0, 2).Value = StrUnitName _
            And .Offset(0, 3) = StrTCsSigned And .Offset(0, 4) = strVetSurveyCompleted _
            And .Offset(0, 5) = StrBiosecurityReceived _
            And .Offset(0, 6) = StrCDSurveyReceived _
            And .Offset(0, 7) = strBPHSOptInOut Then


    End With
    Set rngInventory = rngInventory.Offset(1, 0)
Loop
End Sub

1 个答案:

答案 0 :(得分:0)

这将循环遍历A列中的所有值,从A2开始,并检查D列是否为“是”。如果找到,它会将整行复制到报告表

Sub AnalyseEMPHInfo()
    Dim rNext As Range
    Dim rCell As Range
    Dim rngInventory As Range

    With Worksheets("Inventory")
        Set rngInventory = .Range(.Range("A2"), .Range("A2").End(xlDown))
    End With

    For Each rCell In rngInventory.Cells
        If UCase(rCell.Offset(0, 3).Value) = "YES" Then 'check column D
            With Worksheets("Reports")
                Set rNext = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
            End With

            rCell.EntireRow.Copy rNext
        End If
    Next rCell

End Sub