Excel 应用程序或对象定义错误
我有一个在打开工作簿时运行的查询。如果将查询刷新为新的工作表(面板),我不确定如何正确修改下面的代码以反映此更改。多谢各位 尝试使用J-O中的范围来填充A2中的数据验证,但是现在我得到一个突出显示粗体部分的错误Excel 应用程序或对象定义错误,excel,vba,Excel,Vba,我有一个在打开工作簿时运行的查询。如果将查询刷新为新的工作表(面板),我不确定如何正确修改下面的代码以反映此更改。多谢各位 尝试使用J-O中的范围来填充A2中的数据验证,但是现在我得到一个突出显示粗体部分的错误 ' Select Patient Application.ScreenUpdating = False With Sheets(“panel”) lastrow = Cells(.Rows.Count, "J").End(xlUp).Row With Selection.Val
' Select Patient
Application.ScreenUpdating = False
With Sheets(“panel”)
lastrow = Cells(.Rows.Count, "J").End(xlUp).Row
With Selection.Validation
End With
With .Range("A2").Validation
.Delete
**.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=$J$1:$O$1" & lastrow**
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End With
Application.ScreenUpdating = True
因此,对于VBA中的这些类型的错误,我希望确保可以用
Dim
声明的所有数据都已完成。通过这种方式,我可以查看每个变量,并查看其值,以进行手动错误检查
比如我拿
Formula1:="=$J$1:$O$1" & lastrow
并将其更改为一个公式
字符串,我在开始时将其变暗:
Dim formula as String
然后,在获得最后一行后,可以调试变量并查看它是否等于(例如)
现在,为了手动测试这一点,我尝试添加一个数据验证列表,并将其作为源。这就是我得到错误的地方:
这个错误消息清楚地总结了它。不能有跨多列的数据验证列表。您需要更改设计以匹配Excel的功能
为了进一步检查这一点,我返回并将公式更改为:
formula = "=$J$1:$J$" & lastrow
运行非常平稳,没有错误,“A2”中会出现一个不错的下拉列表
我使用的完整sub是:
Sub Macro1()
Application.ScreenUpdating = False
Dim formula As String
With Sheets("panel")
lastrow = .Cells(.Rows.Count, "J").End(xlUp).Row
formula = "=panel!$J$1:$J$" & lastrow
End With
With Sheets("annovar")
With .Range("A2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=formula
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End With
Application.ScreenUpdating = True
End Sub
您遇到了什么错误?使用
Set lastrow=Cells(.Rows.Count,“J”).End(xlUp)。Row
@ja72-您不需要Set
来指定长(行号),但在前面应该有一个句点(.)。Cells
。抱歉,我以为它是一个范围,但是。Row
返回一个行号。我的错误。基本上我想做的是让A2中的下拉列表只填充J列中的值,但J-O列中有值(从查询返回)。在A2中的数据验证中,用户选择案例,并填充与该案例相关的值。谢谢:)@user3665765所以可能formula=“=$J$1:$J$”&lastrow
是您想要创建的J列下拉列表。您需要添加一些公式来获得K-O列中的相应数据可能是一些索引
和匹配
我在A2中没有得到带有J列值的下拉列表。。。我没有下拉列表。谢谢:)@user3665765我添加了完整的Sub我很抱歉你所说的完整Sub(学习VB)是什么意思。谢谢:)。
Sub Macro1()
Application.ScreenUpdating = False
Dim formula As String
With Sheets("panel")
lastrow = .Cells(.Rows.Count, "J").End(xlUp).Row
formula = "=panel!$J$1:$J$" & lastrow
End With
With Sheets("annovar")
With .Range("A2").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=formula
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End With
Application.ScreenUpdating = True
End Sub