所以我的vba函数应该根据布尔输入为单元格指定不同的值。真正的部分完美无缺,但我对False声明感到困惑。 True部分为单元格分配一个下拉列表,False部分应该删除它,并将其值设置为文本“N / A”。一旦我尝试更改单元格的值(或实际上任何单元格的值,使用范围(“..”)。value命令,代码就会面临错误。
还有其他方法吗? 提前谢谢!
Function sema_list(rng As Range, sema As Boolean)
If sema = True Then
With rng.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=arrangement"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
If sema = False Then
rng.Validation.Delete
rng.Value = "some text"
End If
sema_list = ""
End Function
答案 0 :(得分:1)
函数只能将文本放在调用它的单元格中,因此您必须使用普通子(可能带参数)来执行此操作。 E.g。
Sub x()
sema_list Range("A1:A2"), False
End Sub
Sub sema_list(rng As Range, sema As Boolean)
If sema = True Then
With rng.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="=arrangement"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
If sema = False Then
rng.Validation.Delete
rng.Value = "some text"
End If
End Sub
答案 1 :(得分:0)
我猜你必须在分配任何值之前传递一些范围。 例如:
Range("A1:A10") = "some text"
或者可能是这样的:
Dim example As Range
Set example = Range("A1:A10")
example.Value = 8