2017-03-29 79 views
0

我需要您的幫助。無法弄清楚爲什麼下面的代碼不起作用。我有一個數據透視表,我期待與函數查找和VLOOKUP一個特定的值,並將其添加到特定的細胞:查找文本並返回其值,如果未找到文本,則返回0

enter image description here

數據更新後 - 有沒有關於10國期間的信息 - 19已經有幾天了。在這種情況下,當某些類別值不存在時 - 我想將0值分配給特定的單元格。 我試過才達到其寫入以下內容:

Dim x As Long 
Dim Lookup_Range, RangeA As range 

With Worksheets("Duration") 

Set Lookup_Range = Worksheets("Duration").range("A1:B56") 

On Error Resume Next 
x = Lookup_Range.Find("1 - 9 days", range("A1"), xlValues, xlWhole, xlByColumns, xlNext).Row 

Set RangeA = .range(.Cells(x, 1), .Cells(x + 4, 2)) 

If Not x <> "" Then 

    .Cells(30, 7) = Application.VLookup("State1", RangeA, 2, False) 
    .Cells(31, 7) = Application.VLookup("State2", RangeA, 2, False) 
    .Cells(32, 7) = Application.VLookup("State3", RangeA, 2, False) 
    .Cells(33, 7) = Application.VLookup("State4", RangeA, 2, False) 

Else 

    .Cells(30, 7) = 0 
    .Cells(31, 7) = 0 
    .Cells(32, 7) = 0 
    .Cells(33, 7) = 0 

End If 

On Error Resume Next 
x = Lookup_Range.Find("10 - 19 days", range("A1"), xlValues, xlWhole, xlByColumns, xlNext).Row 

Set RangeA = .range(.Cells(x, 1), .Cells(x + 4, 2)) 

If Not x <> "" Then 

    .Cells(34, 7) = Application.VLookup("State1", RangeA, 2, False) 
    .Cells(35, 7) = Application.VLookup("State2", RangeA, 2, False) 
    .Cells(36, 7) = Application.VLookup("State3", RangeA, 2, False) 
    .Cells(37, 7) = Application.VLookup("State4", RangeA, 2, False) 

Else 

.Cells(34, 7) = 0 
.Cells(35, 7) = 0 
.Cells(36, 7) = 0 
.Cells(37, 7) = 0 

End If 

End With 

滿足IF的第一條件時,它只返回正確的價值觀。

任何幫助,將不勝感激。

+1

「不起作用」是什麼意思?首先註釋掉On Error行並調試任何錯誤。 – SJR

+0

@SJR沒有'On Error Resume Next'我收到錯誤信息「Runtime Error 91:Object variable or with block variable not set」 – Slava32

+0

它突出顯示了行x = Lookup_Range.Find(「10 - 19 days」,range(「 A1「),xlValues,xlWhole,xlByColumns,xlNext)。我想,因爲它找不到單元格爲」10 - 19天「 – Slava32

回答

1

這增加了一些檢查,發現值應避免錯誤。

Sub xy() 

Dim x As Long, r As Range 

Dim Lookup_Range, RangeA As Range 

With Worksheets("Duration") 
    Set Lookup_Range = .Range("A1:B56") 
    Set r = Lookup_Range.Find("1 - 9 days", .Range("A1"), xlValues, xlWhole, xlByColumns, xlNext) 
    If Not r Is Nothing Then 
     x = r.Row 
     Set RangeA = .Range(.Cells(x, 1), .Cells(x + 4, 2)) 
     .Cells(30, 7) = Application.VLookup("State1", RangeA, 2, False) 
     .Cells(31, 7) = Application.VLookup("State2", RangeA, 2, False) 
     .Cells(32, 7) = Application.VLookup("State3", RangeA, 2, False) 
     .Cells(33, 7) = Application.VLookup("State4", RangeA, 2, False) 
    Else 
     .Cells(30, 7) = 0 
     .Cells(31, 7) = 0 
     .Cells(32, 7) = 0 
     .Cells(33, 7) = 0 
    End If 
    Set r = Lookup_Range.Find("10 - 19 days") 
    If Not r Is Nothing Then 
     x = r.Row 
     Set RangeA = .Range(.Cells(x, 1), .Cells(x + 4, 2)) 
     .Cells(34, 7) = Application.VLookup("State1", RangeA, 2, False) 
     .Cells(35, 7) = Application.VLookup("State2", RangeA, 2, False) 
     .Cells(36, 7) = Application.VLookup("State3", RangeA, 2, False) 
     .Cells(37, 7) = Application.VLookup("State4", RangeA, 2, False) 
    Else 
     .Cells(34, 7) = 0 
     .Cells(35, 7) = 0 
     .Cells(36, 7) = 0 
     .Cells(37, 7) = 0 
    End If 
End With 

End Sub 
+0

您剛剛保存了我的項目。非常感謝你!它像我想要的那樣工作。 – Slava32

相關問題