【问题标题】:excel VBA functionexcel VBA函数
【发布时间】:2019-02-18 10:55:54
【问题描述】:

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

有人可以帮忙吗?

【问题讨论】:

  • 您可以使用COUNTIFS(),而不是for 循环。可能更简单、更快。
  • 你确定逻辑正确吗?从描述中我觉得你想要If (ActiveSheet.Range("C" &amp; i).Value = UserForm1.TextBox1.Text And ActiveSheet.Range("F" &amp; i).Value = UserForm1.TextBox2.Text) Thenkontr = TrueExit Function

标签: excel vba


【解决方案1】:

使用.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

【讨论】:

    【解决方案2】:

    我认为这与您计算 endrow 的方式有关,将该行修改为:

    endrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
    

    获取 C 列最后一行的数据。

    【讨论】:

      【解决方案3】:

      为什么不重写正布尔逻辑中的条件

      假设您要检查 both UserForm1 文本(TextBox1TextBox2是否匹配 两列CF)。换句话说,您想检查组合是否存在于某行中。

      那么你应该检查:

      If (ActiveSheet.Range("C" & i).Value = UserForm1.TextBox1.Text _
        And ActiveSheet.Range("F" & i).Value = UserForm1.TextBox2.Text) Then
         matchFound = True
         MsgBox ("Combination Exists in C and F!")
         Exit For
      Else
         matchFound = False;
      End If
      

      找到匹配后中止过滤怎么办?

      Exit For 将在找到匹配项后退出 for 循环。见Excel VBA - exit for loop

      循环遍历范围

      Dim C_to_F_Columns As Range, cRow As Range
      lastrow = Cells(Rows.Count, "C").End(xlUp).Row
      Set C_to_F_Columns = Range("C2:F" & lastrow)
      
      For Each cRow In C_to_F_Columns.Rows
         cValue = cRow.Offset(0,0).Value
         fValue = cRow.Offset(0,2).Value
      
         ' insert your matching logic here
      Next
      

      looping through a range of 2 columns

      【讨论】:

        【解决方案4】:

        不确定文本中是否有空格,因此不满足条件

        尝试使用修剪 TRIM(文本)方法

        【讨论】:

        • 好点!比较 _text_s 总是可以使用一些 trimming 甚至 ignoreCase 使用 UCaseLCase 字符串函数:equalsIgnoreCase = ( LCase(str1) = LCase(str2) )
        猜你喜欢
        • 2017-04-16
        • 2018-03-10
        • 1970-01-01
        • 1970-01-01
        • 2018-01-17
        • 2018-08-18
        • 2013-06-30
        • 2020-12-22
        • 1970-01-01
        相关资源
        最近更新 更多