比较两个不同工作表中的两个数据列,并在另一个工作表中打印差异/不匹配

时间:2019-06-25 13:56:55

标签: excel vba

我试图在两个不同的工作表中比较两个数据列,并获取差异/不匹配项以复制到第三个工作表中。当我在下面运行此代码时,它说没有不匹配,这是错误的,因为存在不匹配。人们会推荐vlookup,多维for循环,匹配等吗?

'Project title variables
Dim currentTitle As String, oldTitle As String

'Use a variable to store the current row we are writing to
Dim rowRep As Long, rowData As Long
rowRep = ROW_REP_START
rowData = 1


'Go through each row
Dim rgCurRow As Range, rng As Range
For Each rgCurRow In rgCurrentData.Rows

    currentTitle = rgCurRow.Cells(1, COL_CURRENTDATA_PROJECTTITLE)


    oldTitle = rgCurRow.Cells(1, COL_OLDDATA_PROJECTTITLE)

    If Not StrComp(currentTitle, oldTitle, vbTextCompare) = 0 Then

        'Get the destination range - where the data will be written
         shResultSheet.Cells(rowRep, COL_REP_OWNINGBUSINESS).Value2 = rgCurrentData.Cells(rowData, COL_CURRENTDATA_OWNINGBUSINESS).Value2
         shResultSheet.Cells(rowRep, COL_REP_BUSINESSSPONSOR).Value2 = rgCurrentData.Cells(rowData, COL_CURRENTDATA_BUSINESSSPONSOR).Value2
         shResultSheet.Cells(rowRep, COL_REP_TECHLEAD).Value2 = rgCurrentData.Cells(rowData, COL_CURRENTDATA_TECHLEAD).Value2
         shResultSheet.Cells(rowRep, COL_REP_TITLE).Value2 = rgCurrentData.Cells(rowData, COL_CURRENTDATA_PROJECTTITLE).Value2
         shResultSheet.Cells(rowRep, COL_REP_DESCRIPTION).Value2 = rgCurrentData.Cells(rowData, COL_CURRENTDATA_DESCRIPTION).Value2
         shResultSheet.Cells(rowRep, COL_REP_CURRENTSTATE).Value2 = rgCurrentData.Cells(rowData, COL_CURRENTDATA_CURRENTSTATE).Value2
         shResultSheet.Cells(rowRep, COL_REP_CURRENTSTATUS).Value2 = rgCurrentData.Cells(rowData, COL_CURRENTDATA_CURRENTSTATUS).Value2

    End If

    'Move to the next report row
    rowRep = rowRep + 1
    rowData = rowData + 1

Next rgCurRow

0 个答案:

没有答案