Excel 如果没有被识别的话
我有一个表单,用户在其中输入一个数字,我希望我的宏检查该数字是否存在于特定列表中,以便执行一些操作。我是这样做的:Excel 如果没有被识别的话,excel,vba,if-statement,Excel,Vba,If Statement,我有一个表单,用户在其中输入一个数字,我希望我的宏检查该数字是否存在于特定列表中,以便执行一些操作。我是这样做的: Private Sub CommandButton3_Click() Dim number As Integer, i As Integer For i = 10 To 10020 If TextBox1.Value = Sheet1.Cells(i, 2).Value Then number = TextBox1.Value End If
Private Sub CommandButton3_Click()
Dim number As Integer, i As Integer
For i = 10 To 10020
If TextBox1.Value = Sheet1.Cells(i, 2).Value Then
number = TextBox1.Value
End If
Next i
MsgBox "A dica número " & TextBox1.Value & " não consta na lista.", vbOKOnly, "Dica não encontrada!"
Exit Sub
'Do the rest of the code here
End Sub
问题是:无论我输入什么数字,消息框总是出现number=TextBox1。永远不会读取值
。
我尝试过手动检查特定输入的值是否与消息框相同,即使它们相同,也从未访问过if/Else
任何帮助都将不胜感激。将代码更改为
Option Explicit
Private Sub TextBox1_Change()
End Sub
Private Sub CommandButton3_Click()
Dim number As Integer, i As Integer
For i = 10 To 10020
If TextBox1.Value = Tabelle1.Cells(i, 2).Text Then
number = TextBox1.Value
Exit Sub
End If
Next i
MsgBox "A dica número " & TextBox1.Value & " não consta na lista.", vbOKOnly, "Dica não encontrada!"
Exit Sub
'Do the rest of the code here
End Sub
更新但也许这才是你真正想要的
Private Sub CommandButton3_Click()
Dim number As Integer
Dim rg As Range
With Sheet1
Set rg = .Range(.Cells(10, 2), .Cells(10020, 2))
End With
Dim rgRes As Range
Set rgRes = rg.Find(TextBox1.Value)
If Not (rgRes Is Nothing) Then
number = TextBox1.Value
Else
MsgBox "A dica número " & TextBox1.Value & " não consta na lista.", vbOKOnly, "Dica não encontrada!"
End If
End Sub
将代码更改为
Option Explicit
Private Sub TextBox1_Change()
End Sub
Private Sub CommandButton3_Click()
Dim number As Integer, i As Integer
For i = 10 To 10020
If TextBox1.Value = Tabelle1.Cells(i, 2).Text Then
number = TextBox1.Value
Exit Sub
End If
Next i
MsgBox "A dica número " & TextBox1.Value & " não consta na lista.", vbOKOnly, "Dica não encontrada!"
Exit Sub
'Do the rest of the code here
End Sub
更新但也许这才是你真正想要的
Private Sub CommandButton3_Click()
Dim number As Integer
Dim rg As Range
With Sheet1
Set rg = .Range(.Cells(10, 2), .Cells(10020, 2))
End With
Dim rgRes As Range
Set rgRes = rg.Find(TextBox1.Value)
If Not (rgRes Is Nothing) Then
number = TextBox1.Value
Else
MsgBox "A dica número " & TextBox1.Value & " não consta na lista.", vbOKOnly, "Dica não encontrada!"
End If
End Sub
首先,文本框中输入的数字将被视为字符串而不是数字。所以你需要把它转换成一个数字,要做到这一点,试试这个
If Val(Me.TextBox1.Value) = Sheet1.Cells(i, 2).Value Then
第二件事,您不需要在单元格中循环,只需检查文本框中输入的数字是否在特定范围内。您可以使用CountIf函数来完成此操作,如下面的示例所示
If Application.CountIf(Sheet1.Range("B10:B10020"), Me.TextBox1.Value) > 0 Then
MsgBox "Found"
Else
MsgBox "Not Found"
End If
首先,文本框中输入的数字将被视为字符串而不是数字。所以你需要把它转换成一个数字,要做到这一点,试试这个
If Val(Me.TextBox1.Value) = Sheet1.Cells(i, 2).Value Then
第二件事,您不需要在单元格中循环,只需检查文本框中输入的数字是否在特定范围内。您可以使用CountIf函数来完成此操作,如下面的示例所示
If Application.CountIf(Sheet1.Range("B10:B10020"), Me.TextBox1.Value) > 0 Then
MsgBox "Found"
Else
MsgBox "Not Found"
End If
在
If
语句中放入Exit For
。实际上,代码中没有ElseIf
。也没有理由让'在此处执行其余的代码
,因为退出子项
将阻止执行达到该点。@Luis您编写的代码要求始终出现消息框。你为什么感到惊讶?在你的If
语句中放入Exit For
。你的代码中实际上没有ElseIf
。也没有理由让'在此处执行其余的代码
,因为退出子项
将阻止执行达到该点。@Luis您编写的代码要求始终出现消息框。为什么你会感到惊讶呢?关于如何使用.Find()
来实现OPs的好处,再多了解一点,这将大大有助于你找到一个好答案。请随时添加.Find
的详细信息。目前我甚至不确定OP真正想要什么。关于使用.Find()
实现OPs好处的更多细节将大大有助于这一问题的解决。请随意添加.Find
的详细信息。目前我甚至不知道OP到底想要什么。谢谢!.CountIf
比If
做得更好@LuisEduardo注意,应用程序.CountIf
与早期绑定的应用程序.WorksheetFunction.CountIf
-后者会很高兴地编译一个打字错误,前者给你智能感知,编译时验证,和可捕获的运行时错误,因为参数无效。谢谢!.CountIf
比If
做得更好@LuisEduardo注意,应用程序.CountIf
与早期绑定的应用程序.WorksheetFunction.CountIf
-后者会很高兴地编译一个打字错误,前者给你智能感知,编译时验证,以及给定无效参数的可捕获运行时错误。