在Excel中编写逻辑问题

我想在Excel中编写一系列条件语句(规则?),以便在构build更复杂的规则引擎之前进行概念validation(在Prolog中)

目标是能够将这些声明应用于不同的文本主体,根据有多less规则被触发,对每个主体进行评分。

我已经分离了文章,以便每个单词都在自己的单元格中。 例如A1字1,A2,字2,A3字3等

示例逻辑语句。

如果“足球”居住在“美国人”的任何一方的5个单词内,那么它得到+5分

如果“曲棍球”居住在“场地”两边的5个字之内,则得到+10分

如果“曲棍球”居住在“冰”的任何一边5字以内,则得到+20分

这可以轻松完成吗? 有人提到嵌套,如果错误vlookups,但这是超出我的理解。

另外,我已经探讨了在其他语言看这个,但我不能代码加上项目的其他部分在Excel中工作,所以我有一种联系在一起。 这些部分确实涉及到macros,所以可以开放的探索这个选项。

你如何在VBA中实现这一点?

谢谢

我会想象这可以清理和重构,但一些快速和肮脏的VBA做这个UDF将是:

 Function word_proximity_test(sentence As String, word1 As String, word2 As String, distance As Integer) As Boolean 'Dim a variant for an array Dim words As Variant 'Dim a string for each array element Dim word As Variant 'Dim some integers for holding the positions of word1 and word2 when found Dim word1pos As Integer, word2pos As Integer 'Dim integer to track position in array Dim arrPos As Integer 'Split the sentence into the array words = Split(sentence, " ") 'Iterate the words array arrPos = 1 For Each word In words 'Test for word1 If word = word1 Then 'set the position for word1 word1pos = arrPos 'Test if the word2 has been found and if the distance between them is within the distance specified If word2pos >= 1 And word1pos - word2pos <= distance Then 'Return true and exit word_proximity_test = True Exit Function End If End If 'Test for word2 If word = word2 Then word2pos = arrPos If word1pos >= 1 And word2pos - word1pos <= distance Then word_proximity_test = True Exit Function End If End If 'increment arrPos arrPos = arrPos + 1 Next word End Function 

将其放在工作簿上的新模块中(保存工作簿),然后在cellformula中使用它,如下所示:

  =word_proximity_test(A1, "football", "american", 5) 

如果A1在五个字之内有足球和美国的话,那么这将会返回真或假。