2016-11-22 129 views
2

我剛剛學習Excel VBA的學校作業。我已經根據某些條件爲某個工作表中的特定單元格分配了一個對象「wtCell」。Excel VBA:第二次分配給對象變量的運行時錯誤'91'

錯誤:「對象變量或帶塊變量未設置」

我得到的分配「wtCell」的錯誤

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 = Worksheets("WardData").Range("F39") ' total population for ward type

是得到highlig由調試器(在第一個ElseIf下)加以限制。我真的很感激任何幫助。

+0

我認爲你的問題在下面的答案中突出顯示(即你需要「設置」任何對象變量)。另一個需要考慮的事情是去除「GoTo」結構。它很難遵循代碼,並可能產生意想不到的後果。您應該能夠使用嵌套if語句實現相同的結果。 – Kyle

+0

謝謝。沒有他們就很容易得到。我在VBA中尋找一個「休息」實現,而GoTo是我發現的第一個,但我不想養成壞習慣。 –

回答

2

嘗試用

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

,並確保您的所有範圍的東西。不僅是這條線。

+1

修好了!謝謝。 –

+0

歡迎。請確保在代碼的其他位置設置「wtCell」。 – Vityata