【发布时间】:2017-09-14 10:32:40
【问题描述】:
我在这里缺少什么?代码应该像单次执行一样工作,也可以在循环中工作。
Public mySheet As Worksheet
Set mySheet= Sheets("CARS")
此行出现错误-->
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(optionList, ",")
错误是-->
VBA 运行时错误 1004 “应用程序定义或对象定义错误
Public Function addDataValidation(row As Long)
Dim optionList(2) As String
optionList(0) = "1"
optionList(1) = "2"
optionList(2) = "3"
With mySheet.Cells(row, 3).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Join(optionList, ",")
End With
With mySheet.Cells(row, 3).FormatConditions.Add(xlCellValue, xlEqual, "=1")
.Font.Bold = True
.Interior.ColorIndex = 4
.StopIfTrue = False
End With
With mySheet.Cells(row, 3).FormatConditions.Add(xlCellValue, xlEqual, "=2")
.Font.Bold = True
.Interior.ColorIndex = 6
.StopIfTrue = False
End With
With mySheet.Cells(row, 3).FormatConditions.Add(xlCellValue, xlEqual, "=3")
.Font.Bold = True
.Interior.ColorIndex = 3
.StopIfTrue = False
End With
With mySheet.Cells(row, 3)
.HorizontalAlignment = xlCenter
.Value = optionList(0)
End With
End Function
不合逻辑行为的原因是该函数与不同类模块中的同名函数混淆了。
解是 1
Private Function addDataValidation(row As Long)
解决方案 2
Check always cells protection status.
【问题讨论】:
-
你在哪里设置
mySheet? -
更新了我的问题。
-
仍然没有告诉我们您在哪里获得资格
mySheet。它在一个模块中吗?您的 UDF 是否在同一个模块中? -
原因是我在不同的类模块中使用了同样命名的方法。我将我的功能从公共更改为私人,这解决了这个问题。非常感谢。你让我开心。
-
不知道我是否添加了任何东西来解决您的问题,但很高兴它有帮助:)
标签: excel vba validation