找出有差异的案例

时间:2017-08-23 07:22:12

标签: excel vba excel-vba

我有一张有七个克朗的床单。前六列有真或假,在最后一栏我不得不在一个陈述中提到虚假案件的标题。以下是excel。 Excel sheet

我试过if else声明但是有太多可能性。由于我是VBA的新手,我不知道任何快捷方式。有什么建议吗?....谢谢

2 个答案:

答案 0 :(得分:0)

以下是您应该尝试的简单代码:

Sub FindDiscrepancies()
Dim lastRow, i, j As Long
Dim discrepancies As String: discrepancies = ""
'find number of last row
lastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastRow
    For j = 1 To 6
        If LCase(Cells(i, j).Value) = "false" Then
            discrepancies = discrepancies & Cells(1, j).Value & ", "
        End If
    Next j
    If discrepancies = "" Then
        Cells(i, 7).Value = "No discrepancies found"
    Else
        Cells(i, 7).Value = "Mismatch found in " & discrepancies
    End If
    discrepancies = ""
Next i

End Sub

答案 1 :(得分:0)

试试这个简单的vba代码,

Sub TEXTJOIN()
Dim i As Long, str As String, k As Long, j As Long
str = ""
j = 0
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
If Application.WorksheetFunction.CountIf(Range("A" & i & ":F" & i), True) = 6 Then
    Cells(i, 7) = "No Discrepancy Found"
Else
    For k = 1 To 6
        If Cells(i, k) = False Then
            str = str & Cells(1, k) & ","
            j = j + 1
        End If
    Next k
    str = Left(str, Len(str) - 1) & " mismatch found"
    Cells(i, 7) = Application.WorksheetFunction.Substitute(str, ",", " and ", j - 1)
    str = ""
    j = 0
End If
Next i
End Sub

enter image description here