排查查找错误

时间:2016-10-17 17:37:59

标签: excel vba excel-vba

出于某种原因,我的代码只返回"如果Find Is Nothing"当我知道价值观在那里。这些值以完全相同的格式(带有前导零的18位数字)存储为所有工作表中的文本。

我试图通过使用这个而不是VLOOKUP来加快报告加载时间,但它只给了我" No"和" 0"而不是适当的答案。任何帮助将不胜感激。 (注意:s和e分别是数据的第一行和最后一行)。

编辑:使用和表单VLOOKUP返回适当的值。

Sub GSLookup(s As Integer, e As Integer)
Dim rng As Range
Dim j As Range
Dim k As Range
Dim rngc As Range
Dim rngm As Range

Worksheets("COOIS").Activate
If Worksheets("COOIS").AutoFilterMode = True Then
      Worksheets("COOIS").AutoFilterMode = False
End If

Worksheets("Mara").Activate
If Worksheets("Mara").AutoFilterMode = True Then
    Worksheets("Mara").AutoFilterMode = False
End If

Worksheets("COOIS").Activate
Set rngc = Worksheets("COOIS").Range("A4:P4")
Set rngc = Worksheets("COOIS").Range(rngc, rngc.End(xlDown))
Worksheets("Mara").Activate
Set rngm = Worksheets("Mara").Range("H4:I4")
Set rngm = Worksheets("Mara").Range(rngm, rngm.End(xlDown))
Worksheets("Scorecard").Activate
Set rng = Worksheets("Scorecard").Range(Worksheets("Scorecard").Cells(s, 4), Worksheets("Scorecard").Cells(e, 4))

For Each j In rng
        Worksheets("Mara").Activate
    If rngm.Find(j, LookAt:=xlWhole) Is Nothing Then
            j.Offset(0, 2).Value = 0
    Else
            Set k = rngm.Find(j.Value, LookAt:=xlWhole)
            j.Offset(0, 2).Value = k.Offset(0, 1).Value
    End If
    Worksheets("COOIS").Activate
    If rngc.Find(j, LookAt:=xlWhole) Is Nothing Then
            j.Offset(0, 3).Value = "NO"
            j.Offset(0, 4).Value = "NO"
    Else
            Set k = rngc.Find(j, LookAt:=xlWhole)
            j.Offset(0, 3).Value = "YES"
            j.Offset(0, 4).Value = k.Offset(0, 12).Value
    End If
Next j
End Sub

1 个答案:

答案 0 :(得分:0)

将它切换到一个VLookup,它工作得很好。

Sub GSLookup(s As Integer, e As Integer)
Dim rng As Range
Dim j As Range
Dim k As Range
Dim ja As String
Dim jb As String
Dim rngc As Range
Dim rngm As Range
On Error Resume Next
Worksheets("COOIS").Activate
If Worksheets("COOIS").AutoFilterMode = True Then
      Worksheets("COOIS").AutoFilterMode = False
End If

Worksheets("Mara").Activate
If Worksheets("Mara").AutoFilterMode = True Then
    Worksheets("Mara").AutoFilterMode = False
End If

Worksheets("COOIS").Activate
Set rngc = Worksheets("COOIS").Range("A4:P4")
Set rngc = Worksheets("COOIS").Range(rngc, rngc.End(xlDown))
Worksheets("Mara").Activate
Set rngm = Worksheets("Mara").Range("H4:I4")
Set rngm = Worksheets("Mara").Range(rngm, rngm.End(xlDown))
Worksheets("Scorecard").Activate
Set rng = Worksheets("Scorecard").Range(Worksheets("Scorecard").Cells(s, 4), Worksheets("Scorecard").Cells(e, 4))

For Each j In rng
ja = ""
ja = Application.WorksheetFunction.VLookup(j, rngm, 2, False)
        If ja = "#N/A" Or ja = "" Then
                j.Offset(0, 2).Value = 0
        Else
                j.Offset(0, 2).Value = ja
        End If
jb = ""
jb = Application.WorksheetFunction.VLookup(j, rngc, 13, False)
        If jb = "#N/A" Or jb = "" Then
            j.Offset(0, 3).Value = "NO"
            j.Offset(0, 4).Value = "NO"
    Else
            j.Offset(0, 3).Value = "YES"
            j.Offset(0, 4).Value = jb
    End If
Next j
End Sub