尝试在指定列中查找值

问题描述

以前的问题 https://www.mrexcel.com/board/threads/run-time-error-13.1163596/page-2#posts

尝试在 C 列“WINGS”中查找指定值,然后跨 3 列然后向下 1 列并用值填充该单元格。 这是在一个案例功能见下文

私有子 TextBox6_Change() Dim FirstAddress As String Dim MyArr 作为变体 将 Rng 调暗为范围 Dim ws as 工作表 Dim I As Long

Set ws = ThisWorkbook.Worksheets("Job Card Master")

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

'Search for a Value Or Values in a range
'You can also use more values like this Array("Dove","Sky")
MyArr = Array("WINGS")

'Search Column or range
With ws.Range("C:C")

    For I = LBound(MyArr) To UBound(MyArr)

        'If you want to find a part of the rng.value then use xlPart
        'if you use LookIn:=xlValues it will also work with a
        'formula cell that evaluates to "ron"

        Set Rng = .Find(What:=MyArr(I),_
                        After:=.Cells(.Cells.Count),_
                        LookIn:=xlFormulas,_
                        LookAt:=xlWhole,_
                        SearchOrder:=xlByRows,_
                        SearchDirection:=xlNext,_
                        MatchCase:=False)

        If Not Rng Is nothing Then
        
        FirstAddress = Rng.Address
            Do
                'mark the cell in the column to the right if "WING" is found
                'Rng.Offset(1,3).Value = "X"
                
            Select Case Me.TextBox6.Value
                    
                Case ("Transit")
                Rng.Offset(1,3).Value = "550mm"
                
                Case ("Sprinter")
                Rng.Offset(1,3).Value = "550mm"
                
                Case ("Master")
                Rng.Offset(1,3).Value = "465mm"
                
                Case ("Movano")
                Rng.Offset(1,3).Value = "465mm"
                
                Case ("NV400")
                Rng.Offset(1,3).Value = "465mm"
                
                Case ("Boxer")
                Rng.Offset(1,3).Value = "465mm"
                
                Case ("Ducato")
                Rng.Offset(1,3).Value = "465mm"
                
                Case ("Relay")
                Rng.Offset(1,3).Value = "465mm"
                
                End Select
        Set Rng = .FindNext(Rng)
            Loop While Not Rng Is nothing And Rng.Address <> FirstAddress
        End If
    Next I
End With

With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With

结束子

解决方法

我想出了以下解决方案:

Private Sub TextBox6_Change()
    Dim FirstAddress As String
    Dim MyArr As Variant
    Dim Rng As Range
    Dim I As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Search for a Value Or Values in a range
    'You can also use more values like this Array("Dove","Sky")
    MyArr = Array("WINGS")

    'Search Column or range
    With Sheets("Blad1").Range("C:C")

        For I = LBound(MyArr) To UBound(MyArr)

            'If you want to find a part of the rng.value then use xlPart
            'if you use LookIn:=xlValues it will also work with a
            'formula cell that evaluates to "ron"

            Set Rng = .Find(What:=MyArr(I),_
                            After:=.Cells(.Cells.Count),_
                            LookIn:=xlFormulas,_
                            LookAt:=xlWhole,_
                            SearchOrder:=xlByRows,_
                            SearchDirection:=xlNext,_
                            MatchCase:=False)

            If Not Rng Is Nothing Then
            
            FirstAddress = Rng.Address
                Do
                    'mark the cell in the column to the right if "WING" is found
                    'Rng.Offset(1,3).Value = "X"
                    
                Select Case Me.TextBox6.Value
                        
                    Case ("Transit")
                    Rng.Offset(1,3).Value = "550mm"
                    
                    Case ("Sprinter")
                    Rng.Offset(1,3).Value = "550mm"
                    
                    Case ("Master")
                    Rng.Offset(1,3).Value = "465mm"
                    
                    Case ("Movano")
                    Rng.Offset(1,3).Value = "465mm"
                    
                    Case ("NV400")
                    Rng.Offset(1,3).Value = "465mm"
                    
                    Case ("Boxer")
                    Rng.Offset(1,3).Value = "465mm"
                    
                    Case ("Ducato")
                    Rng.Offset(1,3).Value = "465mm"
                    
                    Case ("Relay")
                    Rng.Offset(1,3).Value = "465mm"
                    
                    End Select
            Set Rng = .FindNext(Rng)
                Loop While Not Rng Is Nothing And Rng.Address <> FirstAddress
            End If
        Next I
    End With

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub