2016-11-22 8 views
2

私はちょうど学校の割り当てのためにExcel VBAを学習しています。私はいくつかの条件に基づいて特定のワークシートの特定のセルになるようにオブジェクト "wtCell"を割り当てました。Excel VBA:オブジェクト変数への2番目の代入時の実行時エラー '91'

エラー:私は

Sub AddWardData() 

    Dim Cell As Object 
    Dim Ward As Object 

    Dim lngLastRow As Long ' We need to know how many rows of data are in Column J 
    lngLastRow = 0 
    lngLastRow = Range("K" & Rows.Count).End(xlUp).Row ' Complex formula - just ignore the details for now 
    Dim w As Integer ' ward number 
    w = 0 
    Dim wtCell As Object 

    For Each Cell In Worksheets("Data").Range("K2:K" & lngLastRow).Cells ' Iterate thru the cells collection in Row K = a copy 
     w = Val(Mid(Cell.Value, 6, 2)) 
     If (w = 0) Then 
      Cell.Offset(0, 3).Value = "" 
      Cell.Offset(0, 4).Value = "" 
      Cell.Offset(0, 5).Value = "" 
      GoTo no_ward 
     End If 
     For Each Ward In Worksheets("WardData").Range("B4:B46").Cells 
      If (Ward.Value = w) Then 
       Cell.Offset(0, 3).Value = Ward.Offset(0, 4) ' 2015 ward population 
       Cell.Offset(0, 4).Value = Ward.Offset(0, 6) ' ward area 
       Cell.Offset(0, 5).Value = Ward.Offset(0, 10) ' 2015 ward pop density 
       GoTo ward_data_retrieved 
      End If 
ward_data_retrieved: 
     Next 
no_ward: 
     If (Application.CountIf(Worksheets("WardData").Range("B5:B16").Cells, w)) Then 
      Cell.Offset(0, 6).Value = "Urban" 
      Cell.Offset(0, 7).Value = "Urban" 
      wtCell = Worksheets("WardData").Range("F18") ' total population for ward type 
      Cell.Offset(0, 8).Value = wtCell.Value 
      Cell.Offset(0, 9).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
      Cell.Offset(0, 10).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
      Cell.Offset(0, 11).Value = wtCell.Value 
      Cell.Offset(0, 12).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
      Cell.Offset(0, 13).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
     ElseIf (Application.CountIf(Worksheets("WardData").Range("B21:B36").Cells, w)) Then 
      Cell.Offset(0, 6).Value = "Suburban" 
      wtCell = Worksheets("WardData").Range("F39") ' total population for ward type 
      Cell.Offset(0, 8).Value = wtCell.Value 
      Cell.Offset(0, 9).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
      Cell.Offset(0, 10).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
      If (Application.CountIf(Worksheets("WardData").Range("B22:B23").Cells, w)) Then 
       Cell.Offset(0, 7).Value = "OESA" 
       wtCell = Worksheets("WardData").Range("F24") ' total population for ward type 
       Cell.Offset(0, 11).Value = wtCell.Value 
       Cell.Offset(0, 12).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
       Cell.Offset(0, 13).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
      ElseIf (Application.CountIf(Worksheets("WardData").Range("B28:B29").Cells, w)) Then 
       Cell.Offset(0, 7).Value = "RRSA" 
       wtCell = Worksheets("WardData").Range("F30") ' total population for ward type 
       Cell.Offset(0, 11).Value = wtCell.Value 
       Cell.Offset(0, 12).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
       Cell.Offset(0, 13).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
      ElseIf (Application.CountIf(Worksheets("WardData").Range("B34:B36").Cells, w)) Then 
       Cell.Offset(0, 7).Value = "KSSA" 
       wtCell = Worksheets("WardData").Range("F37") ' total population for ward type 
       Cell.Offset(0, 11).Value = wtCell.Value 
       Cell.Offset(0, 12).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
       Cell.Offset(0, 13).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
      End If 
     ElseIf (Application.CountIf(Worksheets("WardData").Range("B41:B46").Cells, w)) Then 
      wtCell = Worksheets("WardData").Range("F46") ' total population for ward type 
      Cell.Offset(0, 8).Value = wtCell.Value 
      Cell.Offset(0, 9).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
      Cell.Offset(0, 10).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
      Cell.Offset(0, 11).Value = wtCell.Value 
      Cell.Offset(0, 12).Value = wtCell.Offset(0, 2).Value ' total area for ward type 
      Cell.Offset(0, 13).Value = wtCell.Offset(0, 6).Value ' avg density for ward type 
      Cell.Offset(0, 6).Value = "Rural" 
      Cell.Offset(0, 7).Value = "Rural" 
     End If 
    Next 

End Sub 

私はどのように把握することはできません「wtCell」の割り当てにエラーを取得しています

「変数またはWithブロック変数が設定されていないオブジェクト」コードの太線を得るが、それはこの行です:

wtCell = Worksheets("WardData").Range("F39") ' total population for ward type

highligを取得(最初のElseIfの下にある)デバッガによってhtedされます。私は本当に助けていただければ幸いです。

+0

あなたの問題は以下の回答で強調表示されていると思います(つまり、オブジェクト変数を「設定」する必要があります)。一般的なプラクティスとして検討するもう一つのことは、 "GoTo"構造を削除することです。コードに従うのが難しく、意図しない結果を招く可能性があります。ネストされたifステートメントで同じ結果を得ることができるはずです。 – Kyle

+0

ありがとうございます。彼らなしで乗り込むのはかなり簡単でした。私はVBAで "中断"の実装を探していました.GoToは私が見つけた最初のものでしたが、悪い習慣に陥ることは望ましくありません。 –

答えて

2

set wtCell = Worksheets("WardData").Range("F39")

そして、あなたが何かにすべての範囲を設定していることを確認してみてください。だけでなく、この行。

+1

それはそれを修正しました!ありがとう。 –

+0

ようこそ。あなたのコードの他の場所にも 'wtCell'を設定してください。 – Vityata

関連する問題