VBA excel搜索多列

时间:2014-11-09 16:56:37

标签: excel vba excel-vba

我正在尝试在多个列中搜索特定值。我下面的代码只搜索D列。每当我改变它时,我都会收到错误。我想搜索D to M列。感谢

Do While (Range("D" & CStr(LSearchRow)).Value) > 0

 'If value in column D = LSearchValue, copy entire row to Sheet2
 If Range("D" & CStr(LSearchRow)).Value = LSearchValue Then

   'Select row in Sheet1 to copy
   Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
   Selection.Copy

   'Paste row into Sheet2 in next row
   Sheets("Sheet2").Select
   Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
   ActiveSheet.PasteSpecial

   'Move counter to next row
   LCopyToRow = LCopyToRow + 1

   'Go back to Sheet1 to continue searching
   Sheets("Sheet1").Select

 End If

 LSearchRow = LSearchRow + 1

Loop

6 个答案:

答案 0 :(得分:0)

我以下列方式重新编写代码,希望您会发现它有用。

Sub searchValue()

Dim LSearchValue As String
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim iColumn As Integer

'I'm looking for string = "5", change it to your value or pass it through procedure parameter
LSearchValue = "5"

LCopyToRow = 1

For iColumn = 4 To 13
    LSearchRow = 1
    While Sheets("Sheet1").Cells(LSearchRow, iColumn).Value > 0
        If Sheets("Sheet1").Cells(LSearchRow, iColumn).Value = LSearchValue Then
            'Select row in Sheet1 to copy
            Rows(LSearchRow).Select
            Selection.Copy

            'Paste row into Sheet2 in next row
            Sheets("Sheet2").Select
            Rows(LCopyToRow).Select
            ActiveSheet.PasteSpecial

            'Move counter to next row
            LCopyToRow = LCopyToRow + 1

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

End Sub

答案 1 :(得分:0)

这可能会让你开始。

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

    LCopyToRow = 1
    LSearchValue = 10

    For rw = 1 To 100

        For Each cl In Range("D" & rw & ":M" & rw)
            If cl = LSearchValue Then
                cl.EntireRow.Copy Destination:=Worksheets("Sheet2").Rows(LCopyToRow & ":" & LCopyToRow)
                LCopyToRow = LCopyToRow + 1
            End If
        Next cl

    Next rw
End Sub

注意:

  1. 不确定如何定义LSearchValue值。我明确地这样做了。
  2. 这里我循环超过100行(D1:D100)。你可以改成适合。
  3. 如果您需要预先检查某个值是否大于0(如原始代码中所示),请添加If...语句

答案 2 :(得分:0)

Sub searchValue()
  Dim LSearchValue As String`
  Dim LSearchRow As Integer
  Dim LCopyToRow As Integer
  Dim iColumn As Integer

  Sheet2.Cells.Clear
  Sheet1.Select

  On Error GoTo Err_Execute

  'user inputs value to be found
  LSearchValue = InputBox("Please enter a value to search for.", "Enter value")
  LCopyToRow = 1

  For iColumn = 4 To 13
    LSearchRow = 1
    While Sheets("Sheet1").Cells(LSearchRow, iColumn).Value > -1
      If Sheets("Sheet1").Cells(LSearchRow, iColumn).Value = LSearchValue Then
        'Select row in Sheet1 to copy
        Rows(LSearchRow).Select
        Selection.Copy

        'Paste row into Sheet2 in next row
        Sheets("Sheet2").Select

        Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
        ActiveSheet.Paste

        'Move counter to next row
        LCopyToRow = LCopyToRow + 1

        'Go back to Sheet1 to continue searching
        Sheets("Sheet1").Select
      End If

      LSearchRow = LSearchRow + 1
    Wend
  Next iColumn

  'Position on cell A3
  Application.CutCopyMode = False
  Range("A3").Select

  MsgBox "All matching data has been copied."
  Exit Sub

Err_Execute:
   MsgBox "An error occurred."
End Sub

我试图实现上述代码。我遇到的所有代码都有两个问题。

  1. While Sheets("Sheet1").Cells(LSearchRow, iColumn).Value > -1代替0。 数据将被复制,但由于正在粘贴重复项,因此会发生错误。

  2. 调试时,这行代码LSearchRow = LSearchRow + 1

  3. 发生错误

答案 3 :(得分:0)

我无法评论,所以我将其添加为答案。

  

While Sheets(“Sheet1”)。Cells(LSearchRow,iColumn).Value> -1工作   而不是0.数据将被复制,但发生错误,因为   正在粘贴重复的文件。

它真的不起作用。 Sheets("Sheet1").Cells(LSearchRow, iColumn).Value > -1对所有行都返回true,这就是您在第LSearchRow = LSearchRow + 1行收到错误的原因(实际上是'溢出'错误)。

我不知道你为什么会这样做。如果对于空单元格,请尝试使用Sheets("Sheet1").Cells(LSearchRow, iColumn).Value <> ""

或者不是在......而是你可以做这样的事情:

Dim i as Long

For i=1 to Sheet("Sheet1").Cells(1,iColumn).End(xlDown).Row 'looping from first cell in column 'iColumn' to last not empty cell
    ...
Next i

答案 4 :(得分:0)

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

答案 5 :(得分:0)

使用Find()方法将是查找匹配值的最有效方法。

Sub Find_Value()
Dim lFirstMatch As Long
Dim rngSearch As Range
Dim rngNextRow As Range
Dim sSearchValue As String

    'Set the Search Value
    sSearchValue = "Something"

    'Find the Search Value
    Set rngSearch = Sheets("Sheet1").Range("D:M").Find(What:=sSearchValue, LookAt:=xlWhole, SearchOrder:=xlByRows)
    If Not rngSearch Is Nothing Then

        'If found preserve the first row
        lFirstMatch = rngSearch.Row
        Do

            'Find the last used row on Sheet2
            Set rngNextRow = Sheets("Sheet2").Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
            If Not rngNextRow Is Nothing Then

                'If a last used row is found, copy the row from Sheet1 to the next available row on Sheet2
                rngSearch.EntireRow.Copy Destination:=rngNextRow.Offset(1, 0).EntireRow
            Else

                'If a last used row is not found, copy the row from Sheet1 to the first row on Sheet2
                rngSearch.EntireRow.Copy Destination:=Sheets("Sheet2").Rows(1)
            End If

            'Find the next value on Sheet1
            Set rngSearch = Sheets("Sheet1").Range("D:M").Find(What:=sSearchValue, LookAt:=xlWhole, After:=rngSearch, SearchOrder:=xlByRows)

        'Stop looking for the Search Value once you have come full circle back to the first value
        Loop While Not rngSearch.Row = lFirstMatch
    End If

End Sub