excel VBA函数

标签 excel vba

我有三个文本的表格,可以将此值放在同一行的不同列中。
这很好用。
我创建了这个函数来验证 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/

相关文章:

python - 使用openpyxl复制工作表时如何保留VBA代码?

excel - 使用 FileSystemObject 复制文件 : ThisWorkbook. 路径给出错误的文件名或编号错误

excel - 基于单元格值动态隐藏列

excel - 如何使用按钮隐藏/取消隐藏列?

vba - 比较单元格值以激活 Msgbox

excel - Access 2013 中的类型转换失败

Excel:如何将用户定义的函数应用于多个元素,然后对它们求和?

c# - 使用下面行而不是上面行的格式插入 Excel 行

excel - 当我不知道上限值时如何在excel vba中使用 'ReDim Preserve'

vba - Excel 范围已复制并转置粘贴到记事本。