VBA excel if用于搜索值的语句

时间:2013-11-26 10:14:48

标签: excel vba excel-2007

我的循环中遇到了一些问题..我的代码中的重要部分:

Sub Search()

inputbox MyInput
Do
  found = getInput(MyInput)
Loop
  if found = true then
    'I want to stop doing the loop (like end sub?)
  elseif found = false then
    'do something
  end if
End Sub

我想如果find = true,那么循环将停止并显示我的输入框(从循环中出来?),因为在我的情况下:

第1循环&gt;发现是真的(应该停在这里),但会继续 <第二循环>发现错误

仅供参考我将if放在循环外面因为我检查了5张纸,并且在我用5张纸进行虚假陈述后返回找到的值

2 个答案:

答案 0 :(得分:0)

这是你在尝试的吗?

Sub Sample()
    Dim myInput As String

    Do
        myInput = InputBox("Type in the word you want to search for:")

        If myInput = "" Then Exit Do

        If LCase(myInput) = "true" Then
            MsgBox "User entered True"
        ElseIf LCase(myInput) = "false" Then
            MsgBox "User entered False"
        Else
            MsgBox "User entered something else"
        End If
    Loop
End Sub

答案 1 :(得分:0)

好的,在我尝试这个之后,它现在可以正常工作

Sub Search()

    Dim i As Integer, x As Integer, n As Integer, counter As Integer, found As Boolean, report As Boolean

    i = 0
    n = ActiveWorkbook.Worksheets.Count
    n = n - 2

    Do

    counter = ActiveSheet.Range("A1").Offset(ActiveSheet.Rows.Count - 1, 0).End(xlUp).Row
    counter = counter + 1
    Range("A" & counter).Select

    MyInput = InputBox("Enter Number")

    found = False
    report = False
    If MyInput = "" Then

    Exit Sub

    Else

        Sheets(1).Select
        For i = 0 To n
            Worksheets(ActiveSheet.Index + 1).Select               
            found = getInput(MyInput)
            If found = True Then
                report = True
            End If
        Next i
        Sheets(1).Select
        If report = False Then
            MsgBox MyInput & " not found"
            Cells(counter, 1).Value = Now
            Cells(counter, 4).Value = MyInput
            Cells(counter, 3).Value = Mid(Cells(counter, 4), 5, 4)
            Cells(counter, 5).Value = "Not Found"
        End If
    End If

    Loop Until MyInput = ""

End Sub

ADDED报表参数,用于存储找到的返回值if if = =

PS:有人可以更改问题标题吗?我认为标题现在不符合我的问题和答案,我不知道如何编辑问题