VBA Excel“对象需要”错误
我的代码给了我一个对象需要424错误在这一行:
lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row
我的完整代码:
Private Sub Worksheet_Change(ByVal Target As Range) ' If Target.Count > 1 Then Exit Sub ' If Target.Column > 2 Then Exit Sub Application.EnableEvents = False If Target.Column = 6 Then If Target.Offset(0, 1).Value <> "" Then MsgBox "You must only fill in one of the two columns" Target.ClearContents GoTo ExitSub End If End If If Target.Column = 7 Then If Target.Offset(0, -1).Value <> "" Then MsgBox "You must only fill in one of the two columns" Target.ClearContents GoTo ExitSub End If End If Dim arrData() As Variant Dim i As Long Dim lngRow As Long Dim myNum As Variant Dim ws As Worksheet myNum = Target.Value If Target.Column = 6 Then With BogieInspectionPoints 'this is a sheet name lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row arrData = .Range("a1:b" & lngRow) End With End If If Target.Column = 7 Then With WagonInspectionPoints 'this is a sheet name lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row arrData = .Range("a1:b" & lngRow) End With End If For i = 1 To lngRow If myNum = arrData(i, 1) Then Cells(Target.Row, 8).Value = arrData(i, 2) Exit For End If Next ExitSub: Application.EnableEvents = True End Sub
看起来这些表单variables没有设置。
您将需要在顶部添加此项。
Dim BogieInspectionPoints as Worksheet Dim WagonInspectionPoints as Worksheet Set BogieInspectionPoints = ActiveWorkbook.Sheets("BogieInspectionPoints") Set WagonInspectionPoints = ActiveWorkbook.Sheets("WagonInspectionPoints")
我假设有其他的代码。 当你添加这行时,所有的With
语句应该使用你发布的代码正确处理。
你用With
语句做什么是简化对象。 而不是写作
BogieInspectionPoints.Range("A1")
'更多的代码
你可以写
With BogieInspectionPoints .Range("A1") End With
它使您不必写出完整的对象名称。