Excel VBA:运行时错误';91';关于对象变量的二次赋值
我正在学习Excel VBA以完成学校作业。基于某些条件,我已将对象“wtCell”指定为特定工作表中的特定单元格 错误:“未设置对象变量或带块变量” 我在第二次分配“wtCell”时出错 我不知道如何在代码中获得粗体行,但它是这一行:Excel VBA:运行时错误';91';关于对象变量的二次赋值,vba,excel,runtime-error,Vba,Excel,Runtime Error,我正在学习Excel VBA以完成学校作业。基于某些条件,我已将对象“wtCell”指定为特定工作表中的特定单元格 错误:“未设置对象变量或带块变量” 我在第二次分配“wtCell”时出错 我不知道如何在代码中获得粗体行,但它是这一行: wtCell=工作表(“WardData”).范围(“F39”)”病房类型的总人口 由调试器高亮显示(在第一个ElseIf下)。我非常感谢您的帮助。请尝试使用 set wtCell=工作表(“WardData”).范围(“F39”) 并确保将所有范围都设置为某个
wtCell=工作表(“WardData”).范围(“F39”)”病房类型的总人口
由调试器高亮显示(在第一个ElseIf下)。我非常感谢您的帮助。请尝试使用
set wtCell=工作表(“WardData”).范围(“F39”)
并确保将所有范围都设置为某个值。不仅仅是这一行。我认为您的问题在下面的答案中已经突出显示(即您需要“设置”任何对象变量)。作为一种普遍做法,另一件需要研究的事情是删除“GoTo”结构。它会导致代码难以遵循,并可能产生意外后果。您应该能够使用嵌套的if语句获得相同的结果。谢谢。没有他们过日子很容易。我在VBA中寻找一个“中断”实现,GoTo是我发现的第一个,但我不想养成坏习惯。这就解决了它!谢谢,欢迎。确保在代码的其他位置也设置
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