我有一个表单在MS Access收集反馈来自4个用户。在用户填写表单后,有一个名为"sResultAll“的变量将所有反馈(来自多个文本框)连接到其中,并将其值传递给一个名为"txtRecommendation”的文本框。
sResultAll = sResult1 & sResult2 & sResult3 & sResult4
txtRecommendation.Value = sResultAll我面临的问题是,有几个用户提供了完全相同的反馈,所以我需要一种方法来消除变量sResultAll中的重复。
提前感谢您的捐款。
发布于 2021-12-10 23:06:54
星期五晚上5点55分,没别的事要做。这是免费赠品:
正如@braX所建议的那样,字典对象很擅长跟踪唯一的字符串,因为它可以使用.Exists函数快速搜索当前键。这使您可以在将字符串添加到集合之前检查是否已输入字符串。
为了改进这个想法,我还建议您在比较字符串之前对它们进行净化。强迫他们都在同一情况下,并删除非字母数字字符。这样,无论空格、标点符号还是大写字母,字符串仍然匹配.
Sub Example()
Const sResult1 As String = "George"
Const sResult2 As String = "Fred"
Const sResult3 As String = "John"
Const sResult4 As String = "gEORGE "
Debug.Print Join(DistinctOf(sResult1, sResult2, sResult3, sResult4), ", ")
'Outputs: George, Fred, John
End Sub
Function DistinctOf(ParamArray Strings() As Variant) As Variant()
Dim AlphaNumericOnly As Object
Set AlphaNumericOnly = CreateObject("VBScript.RegExp")
With AlphaNumericOnly
.Global = True
.MultiLine = True
.Pattern = "[^A-Za-z0-9]+"
End With
Dim Distinct_Strings As Object
Set Distinct_Strings = CreateObject("Scripting.Dictionary")
Dim str As Variant
For Each str In Strings
Dim AO_str As String
AO_str = AlphaNumericOnly.Replace(LCase(str), "")
If Not Distinct_Strings.exists(AO_str) Then Distinct_Strings.Add AO_str, str
Next
DistinctOf = Distinct_Strings.Items
End Functionhttps://stackoverflow.com/questions/70311075
复制相似问题