如何将其写入IF语句

时间:2018-11-28 00:09:00

标签: excel vba excel-vba

我想做的是将大量数据从一个大报告(此问题称为新报告)复制到主报告中。

当只有一行从中获取数据时,下面的代码可以正常工作。但是,不确定有多行具有相同ID的行,因为它仅查看第一行而不是所有行。为什么这是个问题?因为有时以下条件没有在第一行中得到满足(如下面的代码所示)

NewReportCell.Offset(0, -93).Value = "-" And _

NewReportCell.Offset(0, -142) = "YES" Then

Sub Interactv4(Optional ByDummy As Byte)

    Dim PK As Variant
    Dim Concat As String
    Dim RowCount As Long
    Dim nextrow As Long
    Dim tC As String 'tC should represent the column with the concatenate in it
    Dim col(1 To 8) As String
    Dim loc As Range
    Dim NewReportCell As Range

    PK= loc.Offset(0, 2).Value

    If loc.Offset(0, 2).Value = "Not Available" Then
        loc.Offset(0, 80) = "Y"
    Else
        loc.Offset(0, 80) = "N"

        'Line items from the new report
        RowCount = WorksheetFunction.CountIf(NewReport.Range("FW:FW"), PK)
        Set NewReportCell = NewReport.Range("FW:FW").Find(PK, lookat:=xlWhole)
        If RowCount = 1 And _
           NewReportCell.Offset(0, -93).Value = "-" And _
           NewReportCell.Offset(0, -142) = "YES" Then

            'A large amount of data is copied from the daily report based on the primary key given above

现在,要隔离具有相同ID的多行的那些行,我添加以下内容。但是,它并没有完成工作,因为它仍然只复制第一行中的副本。任何帮助将不胜感激。

    Else
        If RowCount > 1 And _
           NewReportCell.Offset(0, -93).Value = "-" And _
           NewReportCell.Offset(0, -142) = "YES" Then

1 个答案:

答案 0 :(得分:0)

看起来像是AutoFilter()的作品:

If loc.Offset(0, 2).Value = "Not Available" Then
    loc.Offset(0, 80) = "Y"
Else
    loc.Offset(0, 80) = "N"

    With NewReport 'reference NewReport sheet
        With .Range("AK1:FW" & .Cells(.Rows.Count, "FW").End(xlUp).Row) ' reference referenced sheet range in columns AK:FW from row 1 down to FW column last not empty value
            .AutoFilter Field:=1, Criteria1:="YES" ' filter referenced sheet on it first column (i.e. column "AK") with "YES" content
            .AutoFilter Field:=50, Criteria1:="-" ' filter referenced sheet on it 50th column (i.e. column "CH") with "-" content
            .AutoFilter Field:=143, Criteria1:=PK ' filter referenced sheet on it 143th column (i.e. column "FW") with PK content
            If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then ' uf any filtered values other than referenced range headers row
                .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).Copy Destination:=myOtherWorkbook.Worksheets("masterData").Range("A2") ' copy referenced range filtered values (skipping headers) to you destination workbook/worksheet
            End If
        End With
        .AutoFilterMode = False
    End With
End If

这假设NewReport工作表的第1行是数据库的标题行