2014-11-14 107 views
0

我試圖運行一個宏,它允許用戶在一次搜索中搜索多達15個值。用戶有時可能只搜索1個值,但最終用戶希望此選項可用。我現在的代碼搜索Sheet1 &中的一個值時,發現它將整行復制到Sheet2這很好。現在我正在嘗試多達15個值。我目前的代碼如下:Excel VBA在一次搜索中搜索多達15個值

 
Sub FindValues() 
    Dim LSearchRow As Integer 
    Dim rw As Integer, cl As Range, LSearchValue As Long, LCopyToRow As Integer 

    Sheet2.Cells.Clear 
    Sheet1.Select 

    On Error GoTo Err_Execute 

'this for the end user to input the required A/C to be searched 

    LSearchValue = InputBox("Please enter a value to search for.", "Enter value") 
    LCopyToRow = 2 

    For rw = 1 To 1555 
     For Each cl In Range("D" & rw & ":M" & rw) 
      If cl = LSearchValue Then 
       cl.EntireRow.Copy 
        'Destination:=Worksheets("Sheet2") 
        '.Rows(LCopyToRow & ":" & LCopyToRow) 
       Sheets("Sheet2").Select 
       Rows(LCopyToRow & ":" & LCopyToRow).Select 
        'Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats 
       Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _ 
       xlNone, SkipBlanks:=False, Transpose:=False 
      'Move counter to next row 
       LCopyToRow = LCopyToRow + 1  
      'Go back to Sheet1 to continue searching 
       Sheets("Sheet1").Select 
      End If 
      'LSearchRow = LSearchRow + 1 

     Next cl 
    Next rw 

'Position on cell A3 
'Application.CutCopyMode = False 
'Selection.Copy 

    Sheets("Sheet2").Select 
    Cells.Select 
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ 
    SkipBlanks:=False, Transpose:=False 
    Application.CutCopyMode = False 

    Sheet2.Select 


    MsgBox "All matching data has been copied." 


    Exit Sub 

Err_Execute: 

    MsgBox "An error occurred." 

End Sub 
+1

我沒有看到您嘗試解釋15個可能的搜索詞的部分。 – Adam 2014-11-14 23:10:08

+0

LSearchValue =的InputBox( 「請輸入一個值來搜索」, 「輸入數值」) LCopyToRow = 2 對於RW = 1至1555 對於每個CL在範圍( 「d」 &RW& 「:M」 &rw) 如果cl = LSearchValue然後 cl.EntireRow.Copy「在這一點上我正在嘗試一個正在工作的值im不知道如何改變它的15個值 – kay 2014-11-14 23:13:15

+0

我希望你不會提示用戶15次,如果是這樣,那麼保存15個值(或他們輸入的數量),然後計算輸入的數值,然後建立循環,以便從1-15(取決於計數)進行檢查,然後計算你有多少匹配如果匹配=用戶輸入,則複製該行 – 2014-11-14 23:17:06

回答

1

請嘗試以下代碼。您可能希望使搜索項的輸入更加健壯,因爲如果他們單擊取消,或輸入任何非數字值,您將收到錯誤。

Option Explicit 

Sub FindValues() 
Dim LSearchRow As Integer 
Dim rw As Integer, cl As Range, LSearchValue As Long, LCopyToRow As Integer 

Dim iHowMany  As Integer 
Dim aSearch(15) As Long 
Dim i   As Integer 

On Error GoTo Err_Execute 

Sheet2.Cells.Clear 
Sheet1.Select 

iHowMany = 0 
LSearchValue = 99 

'this for the end user to input the required A/C to be searched 

Do While LSearchValue <> 0 
    LSearchValue = InputBox("Please enter a value to search for. Enter a zero to indicate finished entry.", "Enter Search value") 
    If LSearchValue <> 0 Then 
     iHowMany = iHowMany + 1 
     If iHowMany > 15 Then 
      MsgBox "You are limited to 15 search numbers.", vbOKOnly, "Limit reached" 
      iHowMany = 15 
      Exit Do 
     End If 
     aSearch(iHowMany) = LSearchValue 
    End If 
Loop 

If iHowMany = 0 Then 
    MsgBox "No selections entered.", vbOKOnly + vbCritical, "No Search data" 
    Exit Sub 
End If 

LCopyToRow = 2 

For rw = 1 To 1555 
    For Each cl In Range("D" & rw & ":M" & rw) 
    '------------------------------------------------ 
     For i = 1 To iHowMany 
      Debug.Print cl.Row & vbTab & cl.column 
      LSearchValue = aSearch(i) 
      If cl = LSearchValue Then 
       cl.EntireRow.Copy 

       'Destination:=Worksheets("Sheet2") 
       '.Rows(LCopyToRow & ":" & LCopyToRow) 

       Sheets("Sheet2").Select 
       Rows(LCopyToRow & ":" & LCopyToRow).Select 

       'Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats 
       Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _ 
        xlNone, SkipBlanks:=False, Transpose:=False 

       'Move counter to next row 
       LCopyToRow = LCopyToRow + 1 

       'Go back to Sheet1 to continue searching 
       Sheets("Sheet1").Select 
      End If 
     Next i 
     'LSearchRow = LSearchRow + 1 
    Next cl 
Next rw 

'Position on cell A3 
'Application.CutCopyMode = False 
'Selection.Copy 

Sheets("Sheet2").Select 
Cells.Select 

Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ 
    SkipBlanks:=False, Transpose:=False 

Application.CutCopyMode = False 
Sheet2.Select 

MsgBox "All matching data has been copied." 

Exit Sub 

Err_Execute: 
MsgBox "An error occurred: " & Err.Number & vbTab & Err.Description 
Exit Sub 
Resume Next 
End Sub 
+0

你是個天才!非常感謝你..你在我的最愛列表中,再次感謝你:) – kay 2014-11-15 00:10:32