gpt4 book ai didi

vba - Excel VBA : how to add data validation list which contain comma included value without using reference of Range

转载 作者:行者123 更新时间:2023-12-02 08:17:55 25 4
gpt4 key购买 nike

我需要向单元格添加一个数据验证列表,其中少数值包含逗号。我有同样的问题,像这样Data Validation to Include Comma Character

但我无法引用范围,因为我正在创建一个新工作簿并为其单元格提供数据验证列表。因此,引用对我不起作用,并且由于某些值包含逗号,因此我无法将 Range 设置为 String 并在 Formula1

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=myStr

我发现我可以用其他字符(虚拟字符)替换逗号,并在填充单元格后用逗号替换该虚拟字符,但问题是如何用逗号替换该虚拟字符?

另一种方法是覆盖 Formula1,以便我可以使用虚拟字符作为分隔符,但我不知道如何实现这一点。

请提出任何解决方案,我的最终目标是创建一个新工作簿并通过Excel VBA填充数据验证列表

最佳答案

让我们对单元格 B9 使用虚拟字符方法。这将设置DV:

Sub InternalString2()
Range("B9").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Replace("alpha%ralpha,beta%waiter,gamma%hammer,delta%faucet","%",Chr(130))
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = False
End With
End Sub

其中 Chr(130)虚拟对象.ShowError 很重要。

现在为了替换虚拟,我们使用事件宏:

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("B9")) Is Nothing Then Exit Sub

Application.EnableEvents = False
Range("B9").Replace What:=Chr(130), Replacement:=","
Application.EnableEvents = True
End Sub

关于vba - Excel VBA : how to add data validation list which contain comma included value without using reference of Range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35682142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com