2017-03-29 7 views
0

私はあなたの助けが必要です。以下のコードが動作しない理由を理解できません。データ更新後テキストを見つけてその値を返し、テキストが見つからない場合は0を返します。

enter image description here

19 - - 10の州の期間についての情報はありません:私は、私は、関数FINDやVLOOKUPと、特定の値を検索し、特定のセルに追加ピボットテーブルを持っていますもう何日?この場合、いくつかのカテゴリ値がない場合 - 特定のセルに0の値を割り当てたいと思います。 私は次のように書いて、それをachiveしようとした:

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条件が満たされたときそれが唯一の正しい値を返します。

ご協力いただければ幸いです。

+1

「機能しません」とはどういう意味ですか?まず、On Error行をコメントアウトし、エラーをデバッグします。 – SJR

+0

@SJR 'On Error Resume Nextなし'エラーメッセージ "ランタイムエラー91:オブジェクト変数またはブロック変数が設定されていません" – 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

関連する問題