搜索号码

时间:2020-04-07 17:46:26

标签: excel vba

我们的团队中有3个系统-一个系统无法识别搜索数字的Excel VBA代码-有什么想法吗?谢谢

代码如下:我们遇到问题的部分是Selection.Find部分: 我见过的最奇怪的事情-谢谢。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim curid As String
Application.Goto Reference:="R2C2"
'curid is the id number to search for
curid = ActiveCell.Value2

  'Target Address is F1 - contains a drop down list of selections
If Target.Address = "$F$1" Then
    On Error GoTo bm_Safe_Exit
    Application.EnableEvents = False
    Select Case Target.Value2
        Case "Approved"
        'statusid is a column containing all record id numbers ranging from 1 to 115 right now
                Application.Goto Reference:="statusid"
        'this is the problem - it will not execute on one system we have
                Selection.Find(What:=curid, After:=ActiveCell, LookIn:=xlValues, LookAt _
                :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                False, SearchFormat:=False).Activate

                ActiveCell.Offset(0, 1).Range("A1").Select
                ActiveCell.FormulaR1C1 = "Approved"
                Sheets("Update Form").Select

1 个答案:

答案 0 :(得分:0)

应该看起来像这样:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim curid As String, f As Range
    curid = Me.Range("B2").Value 'curid is the id number to search for

    'Target Address is F1 - contains a drop down list of selections
    If Target.Address = "$F$1" Then
        On Error GoTo bm_Safe_Exit
        Application.EnableEvents = False
        Select Case Target.Value
            Case "Approved"
            'statusid is a column containing all record id numbers ranging from 1 to 115 right now
                Set f = Range("statusid").Find(What:=curid, LookIn:=xlValues, _
                               LookAt:=xlWhole, MatchCase:=False)
                If Not f Is Nothing Then
                    f.Offset(0, 1).Value = "Approved"
                Else
                    'was not found
                End If