2017-03-18 65 views
1

而不是查找大於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

我在找的名字是「大衛」「安德烈」&「卡羅琳」,不知道我是否寫了三次或使用循環。此外,我無法弄清楚如何在整個電子表格中搜索這些名稱。

+0

打開宏記錄器並使用查找對話框查找名稱。停止錄製並檢查您的代碼。將您想要在3個單元格中找到的名稱放在單獨的工作表上,併爲名爲「NamesToFind」的3個單元格創建一個命名範圍。使用工作表(「您的工作表名稱」),UsedRange爲範圍內的每個名稱(NamesToFind)循環設置使用Range.Find方法來查找名稱。當代碼不起作用時,請回復我們的代碼。注意:你所擁有的代碼不是一個好的起點。 –

+0

@VBABeginner在下面閱讀我的答案和代碼,讓我知道它是否按照你的意圖工作 –

回答

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 

評論:看來你是不是在下面的Select Case進行任何操作的Case Is = vbYes情況:

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 
+0

非常感謝。它是如何「長」而不是「字符串」 – VBABeginner

+0

@VBABeginner什麼是「長」? –

+0

我剛開始學習,所以我可能是錯的,但我認爲龍是數字和字符串是文本。此代碼仍然有效,因此它將我拋棄。然而,我正在試圖找到我可以告訴它在表格中搜索「A1:E30」而不是「C3」的位置。 – VBABeginner