如何将不同表中的msgbox值存储到一张表中

时间:2019-03-14 11:26:59

标签: vba

我想根据从不同工作表中搜索到的多个msgbox值存储到定义范围的工作表中。

我无法使用使用下面的代码。

Private Sub SearchData()
    Dim Wsheet As Worksheet, myCounter
    Dim Loc As Range
    Dim sMsg As String
    Dim strName As String
    On Error Resume Next
    strName = InputBox("Please enter the text you are searching for")
    If strName = "" Then Exit Sub
    For Each Wsheet In ThisWorkbook.Worksheets
        With Wsheet.UsedRange
            Set Loc = .Cells.Find(What:=strName)

            If Not Loc Is Nothing Then

                sMsg = MsgBox("Value is found  in " & Wsheet.Name)
                If MsgBox(sMsg) = vbOK Then
                    ThisWorkbook.Sheets("Instructions").Range("K14").Value = sMsg
                    myCounter = 1
                    Set Loc = .FindNext(Loc)

            End If
        End With
    Next
    If myCounter = 0 Then
        MsgBox ("Value not present in this workbook")
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

代码中间缺少End If

If MsgBox(sMsg) = vbOK Then
    ThisWorkbook.Sheets("Instructions").Range("K14").Value = sMsg
    myCounter = 1
    Set Loc = .FindNext(Loc)
End If '<-- Missing