CountIf Excel VBA标识范围条件满足VBA的位置
是否可以使用CountIf返回VBA内的范围 Application.WorksheetFunction.CountIf(MyRange,x)>1 我想取CountIf在MyRange中找到X的范围,并对其进行操作CountIf Excel VBA标识范围条件满足VBA的位置,vba,excel,Vba,Excel,是否可以使用CountIf返回VBA内的范围 Application.WorksheetFunction.CountIf(MyRange,x)>1 我想取CountIf在MyRange中找到X的范围,并对其进行操作 提前感谢。查找非常快: Sub FindX() Dim r As Range Set r = Cells.Find(What:="X", After:=Range("A1")) If Not r is nothing Then MsgBox r.Address
提前感谢。查找非常快:
Sub FindX()
Dim r As Range
Set r = Cells.Find(What:="X", After:=Range("A1"))
If Not r is nothing Then MsgBox r.Address(0, 0)
End Sub
查找相当快:
Sub FindX()
Dim r As Range
Set r = Cells.Find(What:="X", After:=Range("A1"))
If Not r is nothing Then MsgBox r.Address(0, 0)
End Sub
查找相当快:
Sub FindX()
Dim r As Range
Set r = Cells.Find(What:="X", After:=Range("A1"))
If Not r is nothing Then MsgBox r.Address(0, 0)
End Sub
查找相当快:
Sub FindX()
Dim r As Range
Set r = Cells.Find(What:="X", After:=Range("A1"))
If Not r is nothing Then MsgBox r.Address(0, 0)
End Sub
看来Find可能比CountIF快。我用大量的数据为下面的每一个例子做了说明。有了这个发现,再加上无法操纵countif中的第二个范围,这足以让我继续在代码中使用Find。此外,CountIF不区分大小写!谢谢大家的意见
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A and B turn red
For Each x In Range1
Set y = Range2.Find(What:=x, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If Not y Is Nothing Then
y.Interior.ColorIndex = 3
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
对
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A turn red
For Each x In Range1
If Application.WorksheetFunction.CountIf(Range2, x) > 0 Then
'dunno where Range2 match is - cannot manipulate
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
看来Find可能比CountIF快。我用大量的数据为下面的每一个例子做了说明。有了这个发现,再加上无法操纵countif中的第二个范围,这足以让我继续在代码中使用Find。此外,CountIF不区分大小写!谢谢大家的意见
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A and B turn red
For Each x In Range1
Set y = Range2.Find(What:=x, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If Not y Is Nothing Then
y.Interior.ColorIndex = 3
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
对
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A turn red
For Each x In Range1
If Application.WorksheetFunction.CountIf(Range2, x) > 0 Then
'dunno where Range2 match is - cannot manipulate
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
看来Find可能比CountIF快。我用大量的数据为下面的每一个例子做了说明。有了这个发现,再加上无法操纵countif中的第二个范围,这足以让我继续在代码中使用Find。此外,CountIF不区分大小写!谢谢大家的意见
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A and B turn red
For Each x In Range1
Set y = Range2.Find(What:=x, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If Not y Is Nothing Then
y.Interior.ColorIndex = 3
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
对
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A turn red
For Each x In Range1
If Application.WorksheetFunction.CountIf(Range2, x) > 0 Then
'dunno where Range2 match is - cannot manipulate
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
看来Find可能比CountIF快。我用大量的数据为下面的每一个例子做了说明。有了这个发现,再加上无法操纵countif中的第二个范围,这足以让我继续在代码中使用Find。此外,CountIF不区分大小写!谢谢大家的意见
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A and B turn red
For Each x In Range1
Set y = Range2.Find(What:=x, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
If Not y Is Nothing Then
y.Interior.ColorIndex = 3
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
对
Sub Test()
Dim i As Integer
Dim Range1 As Range
'Range1 values will always have a match in Range2
Dim Range2 As Range
'Range2 values will only appear once and are never duplicated
Dim t As Long
t = Timer
Set Range1 = Range("A1:A100")
Set Range2 = Range("B1:B100000")
For i = 1 To 30000
Cells(i, 1).Value = i
Cells(i, 2).Value = i
Next i
'i overflow error after 30000 or so
'This is the only part of code that changed
'First 100 cells of Column A turn red
For Each x In Range1
If Application.WorksheetFunction.CountIf(Range2, x) > 0 Then
'dunno where Range2 match is - cannot manipulate
x.Interior.ColorIndex = 3
End If
Next x
MsgBox ("done " & Timer)
'find method time = ~56166
'countif method = ~56232
End Sub
不能在函数中真正返回一个范围,除非它是一个UDFI,并且有一个正在使用的宏。Find。我正在考虑使用CountIF来加快速度。我还没有探索过用户定义的函数,但在我的未来会看到很多。你不能只过滤“X”然后处理可见的单元格吗?我可以,但有超过100000行的唯一数据,这是没有效率的。我也不想对原始数据进行更改。无法在函数中真正返回范围,除非它是一个UDFI,并且已经在使用一个工作宏。查找。我正在考虑使用CountIF来加快速度。我还没有探索过用户定义的函数,但在我的未来会看到很多。你不能只过滤“X”然后处理可见的单元格吗?我可以,但有超过100000行的唯一数据,这是没有效率的。我也不想对原始数据进行更改。无法在函数中真正返回范围,除非它是一个UDFI,并且已经在使用一个工作宏。查找。我正在考虑使用CountIF来加快速度。我还没有探索过用户定义的函数,但在我的未来会看到很多。你不能只过滤“X”然后处理可见的单元格吗?我可以,但有超过100000行的唯一数据,这是没有效率的。我也不想对原始数据进行更改。无法在函数中真正返回范围,除非它是一个UDFI,并且已经在使用一个工作宏。查找。我正在考虑使用CountIF来加快速度。我还没有探索过用户定义的函数,但在我的未来会看到很多。你不能只过滤“X”然后处理可见的单元格吗?我可以,但有超过100000行的唯一数据,这是没有效率的。我也不想对原始数据进行更改。虽然查找速度很快,但在我当前的宏中,这是最长的一步。我在几百个细胞的范围内找到每个x,在100000多个细胞的范围内。如果不能使用CountIf,我将在下一步研究UDF。谢谢,不过+1做了一点改变@user2662041:
.Find
足够快。而且在很大范围内绝对比CountIf快。你能给出一个你想要达到的目标的确切例子吗?虽然Find很快,但在我当前的宏中它是最长的一步。我在几百个细胞的范围内找到每个x,在100000多个细胞的范围内。如果不能使用CountIf,我将在下一步研究UDF。谢谢,不过+1做了一点改变@user2662041:.Find
足够快。而且在很大范围内绝对比CountIf快。你能给出一个你想要达到的目标的确切例子吗?虽然Find很快,但在我当前的宏中它是最长的一步。我在几百个细胞的范围内找到每个x,在100000多个细胞的范围内。如果不能使用CountIf,我将在下一步研究UDF。谢谢,不过+1做了一点改变@user2662041:.Find
足够快。而且在很大范围内绝对比CountIf快。你能给出一个你想要达到的目标的确切例子吗?虽然Find很快,但在我当前的宏中它是最长的一步。我在几百个细胞的范围内找到每个x,在100000多个细胞的范围内。如果不能使用CountIf,我将在下一步研究UDF。谢谢,不过+1做了一点改变@user2662041:.Find
足够快。而且在很大范围内绝对比CountIf快。你能举一个确切的例子说明你想要达到的目标吗?