搜索三个名称的工作表

时间:2017-03-18 20:59:06

标签: excel vba excel-vba

而不是查找大于6的数字并将其发送到另一张表。我想查找3个名字,以便我可以搜索联系人列表并将其信息从工作表中提取到报告表。

下面是我的旧代码:

Private Sub CommandButton1_Click()

Dim ws As Worksheet, myCounter
Dim erow, myValue As Long

For Each ws In Sheets
    If ws.Range("C3").Value > 6 Then
        myCounter = 1
        ws.Select
        ws.Range("c3").Select

        myValue = ws.Range("C3").Value
        Worksheets("Report").Select
        erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        ActiveSheet.Cells(erow, 1) = myValue

        nextValue = MsgBox("Value found in " & ws.Name & Chr(10) & "Continue?", vbInformation + vbYesvbNo, ws.Name & " C3 = " & ws.Range("C3").Value)
        Select Case nextValue
            Case Is = vbYes
            Case Is = vbNo
                Exit Sub
        End Select
    End If
Next ws

If myCounter = 0 Then
    MsgBox "None of the sheets contains a " & Chr(10) & "value greater than 6 in cell C3 ", vbInformation, "Not Found"
End If

End Sub

我认为第三行应该是String而不是Long

我正在寻找的名字是“David”“Andrea”& “卡罗琳”,不确定我是否写了三次或使用循环。此外,我无法弄清楚如何在整个电子表格中搜索这些名称。

1 个答案:

答案 0 :(得分:1)

下面的代码将在所有工作表中的单元格“C3”中查找名称“David”,“Andrea”和“Caroline”。对于每个匹配,它会将其复制到“报告”工作表中列A中的第一个空行。

注意:无需使用SelectActiveSheet,而是使用完全有资格的CellsWorksheets

<强>代码

Option Explicit

Private Sub CommandButton1_Click()

Dim ws As Worksheet, myCounter As Long
Dim erow As Long, myValue As Long
Dim nextValue As Long

For Each ws In ThisWorkbook.Sheets
    With ws
        Select Case .Range("C3").Value
            Case "David", "Andrea", "Caroline"
                myCounter = 1 ' raise flag >> found in at least 1 sheet

                ' get first empty row in "Report" sheet
                erow = Worksheets("Report").Cells(Worksheets("Report").Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                Worksheets("Report").Cells(erow, 1) = .Range("C3").Value

                nextValue = MsgBox("Value found in " & .Name & Chr(10) & "Continue?", vbInformation + vbYesNo, .Name & " C3 = " & .Range("C3").Value)
                Select Case nextValue
                    Case Is = vbYes ' <-- if you are not doing anything here, you don't need it >> maybe you don't need the entire `Select Case` here
                    Case Is = vbNo
                        Exit Sub
                End Select
        End Select ' Select Case .Range("C3").Value
    End With
Next ws

If myCounter = 0 Then
    MsgBox "None of the sheets contains the names " & Chr(10) & " 'David', 'Andrea', 'Caroline' in cell C3 ", vbInformation, "Not Found"
End If

End Sub

评论:对于Case Is = vbYesSelect Case中的nextValue = MsgBox("Value found in " & .Name & Chr(10) & "Continue?", vbInformation + vbYesNo, .Name & " C3 = " & .Range("C3").Value) Select Case nextValue Case Is = vbYes ' <-- if you are not doing anything here, you don't need it >> maybe you don't need the entire `Select Case` here Case Is = vbNo Exit Sub End Select ,您似乎没有做任何事情:

If MsgBox("Value found in " & .Name & Chr(10) & "Continue?", vbInformation + vbYesNo, .Name & " C3 = " & .Range("C3").Value) = vbNo Then
    Exit Sub
End If

您可以用以下内容替换整个内容:

!