我正在尝试确定一列中与特定字符串匹配的项目数。我希望以后可以使用该数字来调整数组大小。
我一直在尝试各种使用countif函数的方法。
Sub testMatrix()
Dim nm1 As String
Dim nm2 As String
Dim tbl As ListObject
Dim nm1Count As Double
Dim nm2Count As Integer
nm1 = "teleport 1"
nm2 = "user2"
Set tbl = ActiveSheet.ListObject("Table1")
nm1Count = Application.WorksheetFunction.CountIf(Range("Table1[username]"), nm1)
End Sub
我想结束的变量nm1Count
等于表的username
列中出现字符串“ teleport 1”的次数。因此,对于我的具体工作表,应该说4。
当前,到达nm1Count = line
时会出错并提示
对象不支持此属性或方法
答案 0 :(得分:0)
我愚蠢地忘记了ListObjects末尾的。这是更正的代码。
Sub testMatrix()
Dim nm1 As String
Dim nm2 As String
Dim tbl As ListObject
Dim nm1Count As Long
Dim nm2Count As Long
nm1 = "teleport 1"
nm2 = "user2"
ActiveSheet.Activate
Set tbl = ActiveSheet.ListObjects("Table1")
nm1Count = Application.WorksheetFunction.CountIf(tbl.DataBodyRange.Columns(1), nm1)
nm2Count = Application.WorksheetFunction.CountIf(tbl.DataBodyRange.Columns(1), nm2)
End Sub