搜索行的剪贴板值,找到值时,使用cliboard值过滤列

时间:2018-02-20 07:25:12

标签: excel vba search filter rows

有点新手,但在没有帮助的情况下,我认为我能做到这一点:

我试图创建一个宏来执行以下操作:

1)在列(B)中搜索用户输入的值

2)当找到(1)中的值时,复制活动行的列U中的单元格内容

3)在列U之后搜索其余行,以获取在(2)

中复制的值

4)如果在行中再次找到(2)中复制的值(仅在列U之后的第1次重复),则找到具有相同值的单元格的过滤列

看起来我有步骤1& 2工作,不确定步骤3是否工作或失败,因为循环从A列开始,当它遇到U列中的剪贴板值时停止(而不是在U列之外搜索)。第4步肯定不起作用。

感谢您在解决第3步和第4步时提供的所有帮助。

使用下面的示例,如果用户在MyFind / Input框中输入9,则将搜索B列以在单元格B2中找到9(B列中的值将始终是唯一的)。将搜索第2行的其余部分(在列B之后)以找到在单元格E2中重复的9。然后用9作为标准过滤E列。

.. BC DE F G

1 | 1 2 3 4 5 6 7

2 | 1 9 3 4 9 9 7

3 | 1 8 3 4 9 6 7

4 | 1 3 3 4 5 6 7

我的代码:

Sub LCOUN_Search()
Dim OrgNum As Integer
LastRow = Cells(Rows.Count, "B").End(xlUp).Row


MyFind = InputBox("Please enter 8 digit employee number:") 
    If MyFind = "" Then End

For i = LastRow To 1 Step -1
   If Range("B" & i).Value = MyFind Then
      Range("B" & i).Select
   End If
Next i

Cells(ActiveCell.Row, "U").Select
Selection.Copy

LastCol = Cells(ActiveCell.Row, "V").End(xlToLeft).Column  
*'?Column V so that it searches from after column U?*

For i = LastCol To 1 Step -1
        If Cells(ActiveCell.Row, i) = Selection Then
        ActiveCell.Select
        Selection.Copy

        End If
Next i

Columns(ActiveCell.Column).AutoFilter Field:=1, Criteria:=Selection

End Sub

我也试过这个:

Sub LCOUN_Search()

Dim Found As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row


MyFind = InputBox("Please enter 8 digit employee number:")
    If MyFind = "" Then End

For i = LastRow To 1 Step -1
   If Range("B" & i).Value = MyFind Then
      Range("B" & i).Select
   End If
Next i


Cells(ActiveCell.Row, "U").Select
Found = Selection.Copy

FirstCol = Cells(ActiveCell.Row, "V").Column      
LastCol = ActiveSheet.UsedRange.Column + _
                ActiveSheet.UsedRange.Columns.Count - 1


For k = FirstCol To LastCol
    If Cells(ActiveCell.Row, k) = Selection Then Exit For 
Next k

If k <= LastCol Then                             
     With ActiveSheet.UsedRange
          .AutoFilter Field:=k, Criteria1:=Cells(ActiveCell.Row, k).contents
    End With
End If
End Sub

2 个答案:

答案 0 :(得分:0)

正如我所看到的,你向错误的方向搜索:你从列开始&#34; V&#34;并回到A栏。)

如果你想过滤&#34; B&#34;那么你应该申请Field:=2

试试这个:

FirstCol = Cells(i, "V").Column      ' employee is found in row i
LastCol = Activesheet.Usedrange.Column + _  
                Activesheet.Usedrange.Columns.Count - 1

For k = FirstCol to LastCol
    If Cells(i, k) = MyFind Then Exit For ' break the loop when found
Next k

If k <= LastCol Then                              ' found
     With Activesheet.Usedrange
          .AutoFilter Field:=2, Criteria1:=MyFind    ' column B
          .AutoFilter Field:=k, Criteria1:=MyFind
     End With
End If

您可以选择使用.Find,然后您可以在行的其余部分搜索该值,因此命令为:

Dim rFound As Range
Set rFound = Range(Cells(i, FirstCol), CElls(i, LastCol)).Find(What:=MyFind, LookIn:=xlValues, LookAt:=xlWhole)
If Not rFound Is Nothing then       ' found (1st match after U)
     ' here rFound.Column gives you the column of found cell

由于要求改变而得到的回答:从C列到U列搜索一行,就像这样改变第一列和最后一列,它就能满足您的需求。

FirstCol = 3           ' C
LastCol = Cells(i, "U").Column  ' it's not easy to memorize which column is U

答案 1 :(得分:0)

这可以整理,但会显示Find函数

的示例
Option Explicit

Private Sub LCOUN_Search()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lastRow As Long

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Sheet1")             'change as appropriate
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row

    Dim myFind As String

ResumeLine:
    On Error Resume Next
    myFind = InputBox("Please enter 8 digit employee number or press Cancel to quit.") 'tested with user input 12345686
    On Error GoTo 0

    If myFind = vbNullString Then
        End
    ElseIf Len(myFind) < 8 Or Len(myFind) > 8 Then
        MsgBox "Please enter an 8 digit employee number"
        GoTo ResumeLine
    End If

    Dim foundCell As Range
    Dim initialSearchRange As Range
    Set initialSearchRange = ws.Range("B1:B" & lastRow)

    With initialSearchRange

        Set foundCell = .Find(myFind, LookIn:=xlValues)

        If Not foundCell Is Nothing Then

            Dim foundRow As Long
            foundRow = foundCell.Row
            Set foundCell = Nothing

            Dim lastColumn As Long
            lastColumn = ws.Cells(foundRow, ws.Columns.Count).End(xlToLeft).Column

            myFind = ws.Range("U" & foundRow)

            If myFind = vbNullString Then
                MsgBox "Value not found after column U"
                End
            End If

            Dim newSearchRange As Range
            Set newSearchRange = ws.Range(ws.Cells(foundRow, "V"), ws.Cells(foundRow, lastColumn))

            Set foundCell = newSearchRange.Find(myFind, LookIn:=xlValues)

            Dim filterValue As String

            If Not foundCell Is Nothing Then

                filterValue = CStr(foundCell)    'this depends on what datatype you are expecting to have returned
                ws.Range(ws.Cells(1, "A"), ws.Cells(lastRow, lastColumn)).AutoFilter Field:=foundCell.Column, Criteria1:=filterValue

            End If

        End If

    End With


End Sub

参考文献:

1)How to find a value in an excel column by vba code Cells.Find

2)Find last row, column or last cell

下面的示例过滤器(道歉但是在图像中粘贴SO的常用方法似乎不起作用,因此获得了大量带图像的空白区域):

Example filter

请注意,上面没有合适的标题。你的例子没有提到这个。如果在设置范围时要记住标题,则可能需要使用row = 1或row = 2,而不是像提供的代码中那样只使用row = 1。这里的原则是使用Find来实现您描述的步骤。