VBA 使用 countifs 误差和动态范围
VBA using countifs error and dynamic ranges
我在 A4:B27 中有以下一组 mock-data,第 4 行包含 headers..
4. Entity Country
5. 12 countryb
6. 13 dave
7. 14 dan
8. 15 john
9. 16 james
10. 17 josh
11. 18 george
12. 19 geni
13. 20 gina
14. 10 countrya
15. 10 countrya
16. 11 country
17. 12 countryb
18. 12 countryb
19. 13 brian
20. 14 ryan
21. 15 louis
22. 16 tom
23. 17 chris
24. 18 mad
25. 19 barb
26. 20 james
27. 10 countrya
在 VBA 中,我想确保没有重复的 entity-country 组合。这在工作表中很容易看出,公式为“=COUNTIFS($A$5:$A$27,A5,$B$5:$B$27,B5)”。如果返回值大于 1,我想突出显示 entity-country 单元格以显示重复项。在上面的示例中,第 5、14、15、17、18 和 27 行将突出显示。
但是在尝试创建 VBA 之后我卡住了..
Sub test()
Dim cSheet As Worksheet
Set cSheet = Sheets("CL.AL1")
Dim trolSheet As Worksheet
Set trolSheet = Sheets("Control Sheet")
Dim currentRow As Integer, lastRow As Integer, currentColumn As Long
Dim listA As range, listB As range, cellA As String, cellB As String
cSheet.Select
currentColumn = 1
currentRow = 5
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Set listA = range(Col_Letter(currentColumn) & currentRow & ":" & Col_Letter(currentColumn + 1) & lastRow)
Set listB = range(Col_Letter(currentColumn + 1) & currentRow & ":" & Col_Letter(currentColumn + 1) & lastRow)
Do While range("A" & currentRow) <> ""
cellA = (cSheet.range(Col_Letter(currentColumn) & currentRow).Value)
cellB = (cSheet.range(Col_Letter(currentColumn + 1) & currentRow).Value)
If WorksheetFunction.CountIfs(listA, cellA, listB, cellB) > 1 Then
Union(range(Col_Letter(currentColumn) & currentRow), _
range(Col_Letter(currentColumn + 1) & currentRow)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
If currentRow = lastRow Then
currentRow = 5
currentColumn = currentColumn + 1
If currentColumn = 3 Then
Exit Do
End If
Else
currentRow = currentRow + 1
End If
Loop
Debug.Print (range(Col_Letter(currentColumn) & currentRow).Value)
Debug.Print (range(Col_Letter(currentColumn + 1) & currentRow).Value)
End Sub
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
执行当前 VBA 后,我收到运行时 1004 错误 "unable to get the countifs property of the worksheet function class"。
所以。任何人都可以帮助纠正此错误或提供替代解决方案吗?
提前致谢。
将其作为条件格式的公式输入到单元格 A4 中,然后将格式复制到该列的其余部分。
=COUNTIFS($A:$B,A4)>1
我在 A4:B27 中有以下一组 mock-data,第 4 行包含 headers..
4. Entity Country
5. 12 countryb
6. 13 dave
7. 14 dan
8. 15 john
9. 16 james
10. 17 josh
11. 18 george
12. 19 geni
13. 20 gina
14. 10 countrya
15. 10 countrya
16. 11 country
17. 12 countryb
18. 12 countryb
19. 13 brian
20. 14 ryan
21. 15 louis
22. 16 tom
23. 17 chris
24. 18 mad
25. 19 barb
26. 20 james
27. 10 countrya
在 VBA 中,我想确保没有重复的 entity-country 组合。这在工作表中很容易看出,公式为“=COUNTIFS($A$5:$A$27,A5,$B$5:$B$27,B5)”。如果返回值大于 1,我想突出显示 entity-country 单元格以显示重复项。在上面的示例中,第 5、14、15、17、18 和 27 行将突出显示。
但是在尝试创建 VBA 之后我卡住了..
Sub test()
Dim cSheet As Worksheet
Set cSheet = Sheets("CL.AL1")
Dim trolSheet As Worksheet
Set trolSheet = Sheets("Control Sheet")
Dim currentRow As Integer, lastRow As Integer, currentColumn As Long
Dim listA As range, listB As range, cellA As String, cellB As String
cSheet.Select
currentColumn = 1
currentRow = 5
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Set listA = range(Col_Letter(currentColumn) & currentRow & ":" & Col_Letter(currentColumn + 1) & lastRow)
Set listB = range(Col_Letter(currentColumn + 1) & currentRow & ":" & Col_Letter(currentColumn + 1) & lastRow)
Do While range("A" & currentRow) <> ""
cellA = (cSheet.range(Col_Letter(currentColumn) & currentRow).Value)
cellB = (cSheet.range(Col_Letter(currentColumn + 1) & currentRow).Value)
If WorksheetFunction.CountIfs(listA, cellA, listB, cellB) > 1 Then
Union(range(Col_Letter(currentColumn) & currentRow), _
range(Col_Letter(currentColumn + 1) & currentRow)).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End If
If currentRow = lastRow Then
currentRow = 5
currentColumn = currentColumn + 1
If currentColumn = 3 Then
Exit Do
End If
Else
currentRow = currentRow + 1
End If
Loop
Debug.Print (range(Col_Letter(currentColumn) & currentRow).Value)
Debug.Print (range(Col_Letter(currentColumn + 1) & currentRow).Value)
End Sub
Function Col_Letter(lngCol As Long) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
执行当前 VBA 后,我收到运行时 1004 错误 "unable to get the countifs property of the worksheet function class"。
所以。任何人都可以帮助纠正此错误或提供替代解决方案吗? 提前致谢。
将其作为条件格式的公式输入到单元格 A4 中,然后将格式复制到该列的其余部分。
=COUNTIFS($A:$B,A4)>1