我试图调试这段代码很长一段时间已经有人知道它的问题是什么?
代码:
Sub Find_Field_List()
Dim Last_Field As Integer
Dim Field_List() As Variant
Last_Field = Summary_File.Sheets("Settings").Cells(1, 1).End(xlDown).Row
Field_List() = Summary_File.Sheets("Settings").Range(Cells(2, 1), Cells(Last_Field, 1))
End Sub
错误(以Field_List()
开头的高亮显示行):
RunTime Error 1004
Application-Defined or Object-Defined Error
立即窗口:
?Summary_file.Sheets(2).Name
Settings
Split MBSA.xlsm
?Range(Cells(2,1),Cells(5,1)).Count
4
?Last_Field
5
?Summary_File.Sheets(2).Range(Cells(1,1))
Fields
答案 0 :(得分:3)
我认为原因很简单。代码中的错误是单元格Cells(2, 1)
和Cells(Last_Field, 1)
不是完全限定的。而且您收到此错误是因为代码运行时Summary_File.Sheets("Settings")
未处于活动状态。因此,应始终完全限定对象。
试试这个。请注意以下代码中的 DOT 。
Sub Find_Field_List()
Dim Last_Field As Integer
Dim Field_List() As Variant
With Summary_File.Sheets("Settings")
Last_Field = .Cells(1, 1).End(xlDown).Row
Field_List = .Range(.Cells(2, 1), .Cells(Last_Field, 1)).Value
End With
End Sub
修改强>
还有一个提示:尝试并避免使用.End(xlDown).Row
您最终可能会选择整个列!如果您只想选择最后一行,那么您可能希望看到THIS