简体   繁体   English

不区分大小写的VBA脚本

[英]Not case sensitive vba script

I need to make script not be case sensitive when executing if clause.. I've placed the "compare.mode=vbTextcompare not working " on different location in the script in order to solve this problem but is not working in my case... There is no error or any other problem with script.. The comparison is between a range of name all starting with capital letter but the matching part is with lower case... any solutions for this? 我需要使脚本在执行if子句时不区分大小写。为了解决此问题,我在脚本中的其他位置放置了“ compare.mode = vbTextcompare不起作用”,但在我的情况下不起作用。 。脚本没有错误或任何其他问题。比较之间的范围是全部以大写字母开头的名称,但匹配的部分以小写字母...对此的任何解决方案?

Sub DropDown14_Change()
Dim ScCell As Range, key
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Set ws = Worksheets("Dashboard")
Set wsD = Worksheets("DATA")
Set complistDict = CreateObject("Scripting.Dictionary")
Dim DD14V As Variant



complistDict.RemoveAll
Dim DD14 As Object
Set complistDict = Nothing
Set complistDict = CreateObject("scripting.dictionary")

ws.Shapes("Drop Down 16").ControlFormat.RemoveAllItems

Set DD14 = ws.Shapes("Drop Down 14").OLEFormat.Object
Set DD16 = ws.Shapes("Drop Down 16").OLEFormat.Object
DD14V = DD14.List(DD14.Value)

'ws.dropdown14.Clear
 For Each ScCell In wsD.Range("E2", wsD.Cells(Rows.Count, "E").End(xlUp))
 Dic.CompareMode = vbTextCompare
If ScCell.Value = DD14V Then
If Not Dic.Exists(LCase(rCell.Offset(, -1).Value)) Then
Dic.Add LCase(rCell.Offset(, -1).Value), Nothing

End If

End If
Next ScCell
'MsgBox DD14.List(DD14.ListIndex)

 For Each key In Dic
 DD16.AddItem key
 Next



 End Sub

If I understand you correctly, you want to make 如果我理解正确,您想

If ScCell.Value = DD14V Then

be case insensitive. 不区分大小写。

You can either place 您可以放置

Option Compare Text

at the beginning of your macro, before the first Sub statement. 在宏的开头,在第一个Sub语句之前。

Or, to limit the case insensitivity to just that line, you can use: 或者,要将大小写不敏感限制为仅该行,可以使用:

If Ucase(ScCell.Value) = Ucase(DD14V) Then

But your code seems to have other errors. 但是您的代码似乎还有其他错误。

I would suggest you place Option Explicit at the beginning of your macro (before the first Sub ) which will help with debugging. 我建议您将Option Explicit放在宏的开头(在第一个Sub之前),这将有助于调试。

I don't understand why MS makes NOT requiring variable declaration the default. 我不明白为什么MS将默认情况下不要求变量声明。 Select Tools/Options/Editor and check Require Variable Declaration . 选择Tools/Options/Editor然后选中Require Variable Declaration This will place Option Explicit at the start of any new module. 这会将Option Explicit放置在任何新模块的开头。

It may also help to use Early binding instead of Late binding for your dictionary object to get the benefit of Intellisense. 为字典对象使用“早期”绑定而不是“后期”绑定也可能会有所帮助,以获得Intellisense的优势。

According to this 根据这个

Try to use 尝试使用

Option Compare Text

On top of you code. 在您的代码之上。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM