我有三个文本的表格,可以将此值放在同一行的不同列中。
这很好用。
我创建了这个函数来验证 text1 和 text2 的组合是否存在于某行上。
但它只验证第一个条件。
Function kontr() As Boolean
Dim endRow As Long
endRow = ActiveSheet.Range("C:C").End(xlUp).Row
For i = 1 To endRow
If (ActiveSheet.Range("C" & i).Value <> UserForm1.TextBox1.Text And ActiveSheet.Range("F" & i).Value <> UserForm1.TextBox2.Text) Then
kontr = True
Else
MsgBox ("Exists!")
kontr = False
End If
Next i
End Function
有人可以帮忙吗?
最佳答案
使用.Find
.它更快。您的代码同时检查同一行。我猜你想检查这些值是否存在 anywhere
在相应的列中。
这是你正在尝试的吗?
Public Function kontr() As Boolean
Dim ws As Worksheet
Set ws = ActiveSheet
With ws
Set aCell = .Columns(3).Find(What:=UserForm1.TextBox1.Text, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
Set bCell = .Columns(5).Find(What:=UserForm1.TextBox2.Text, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If aCell Is Nothing And bCell Is Nothing Then
kontr = True
Else
MsgBox ("Exists!")
kontr = False
End If
End With
End Function
关于excel VBA函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54745675/