条件格式:在 Excel for Mac 中使用 3 种颜色标度显示文本相关性

条件格式:在 Excel for Mac 中使用 3 种颜色标度显示文本相关性

有没有办法在 Excel 中以以下方式使用条件格式?(对于那些好奇的人,我使用的是 Mac 版 Excel 2010)

我希望当 A2 说“约翰喜欢青苹果”并且 B2 也说“约翰喜欢青苹果”时,单元格 B2 为绿色;如果 A2 说“约翰喜欢青苹果”并且 B2 说“比尔喜欢蓝苹果”则为黄色;如果 A2 说“约翰喜欢青苹果”并且 B2 说“比尔讨厌红橙子”则为红色。

我只是想知道是否有办法实现这一点。

谢谢

答案1

当然可以。选择单元格 B2 并转到 标签 >样式群组 >条件格式>新规则>使用公式确定要格式化的单元格

输入此公式:

=AND(A2="John likes green apples",B2="John likes green apples")

设置格式为填充绿色。

对每个附加颜色和规则重复上述操作。

答案2

这肯定需要 VBA。以下函数将返回匹配值的 0-1 值(即百分比)处于同一位置。使用方法与使用内置函数相同。然后,​​您可以将常规条件格式应用于此值。

它有一个可选的 True/False(默认为 false),表示是否应该区分大小写。

“Bob ate Dinner” 与 “Bob ate Dinner.” 的结果为 0.66,因为它没有忽略标点符号。

“Bob Bob Bob Bob” vs “Bob Bob” = 2/4 = 0.5——当字符串包含不同数量的单词时,两者中较大的一个是分母,但分子最大值为两者中较小的一个。

Function ScorePair(stringInput As String, stringTarget As String, Optional caseSensitive As Boolean = False) As Double

Dim scoreNum As Integer
Dim scoreDen As Integer
Dim splitInput As Variant
Dim splitTarget As Variant
Dim theScore As Double
Dim sizeTarget As Integer
Dim sizeInput As Integer
Dim loopSize As Integer

'Initialize
scoreNum = 0
i = 0

'Extract strings into arrays of words
splitInput = Split(stringInput, " ")
splitTarget = Split(stringTarget, " ")

'Get sizes of arrays to know how much to loop (smaller of two) and what to set denominator at (bigger of two)
sizeInput = UBound(splitInput, 1)
sizeTarget = UBound(splitTarget, 1)
scoreDen = WorksheetFunction.Max(sizeTarget, sizeInput) + 1
loopSize = WorksheetFunction.Min(sizeTarget, sizeInput)

'Loop through arrays comparing them by matching position
For i = i To loopSize
    If caseSensitive Then
        If splitInput(i) = splitTarget(i) Then
            scoreNum = scoreNum + 1
        End If
    ElseIf LCase(splitInput(i)) = LCase(splitTarget(i)) Then
        scoreNum = scoreNum + 1
    End If
Next

'Calculate the score as percentage
theScore = scoreNum / scoreDen
ScorePair = theScore

End Function

答案3

你的问题也不完全清楚。实际的名字重要吗(约翰、比尔)?水果重要吗?还是关于 3 个条件

  1. 名字匹配,对水果的感觉匹配
  2. 名字对不上,但对水果的感觉对得上
  3. 名字不对,对水果的感觉也不对

我假设唯一重要的是人的名字,以及他们是否对某事有相同的感觉。因此,人的名字可以是任何东西,如果比尔和克莱尔都讨厌这种水果,我假设这也是一个橘子。你现在有代码了,所以修改起来应该很容易!

Sub Button1_Click()

Dim row As Integer
row = 1
Do While (True)

If Range("A" & row).Value = "" Then
    Exit Do
End If

Dim score As Integer
score = 0

Dim wordsA() As String
wordsA = Split(Range("A" & row).Value, " ")

Dim wordsB() As String
wordsB = Split(Range("B" & row).Value, " ")

If wordsA(0) = wordsB(0) Then
    score = score + 1
End If

If wordsA(1) = wordsB(1) Then
    score = score + 1
Else
    score = score + 3
End If


'2 = both same name, both feel same way
'1 = different name, both feel same way
'3 = differet feelings

Select Case score
   Case 1
   Case 4
Range("B" & row).Interior.ColorIndex = 46

   Case 2
   Range("B" & row).Interior.ColorIndex = 43

   Case 3
Range("B" & row).Interior.ColorIndex = 30


End Select

row = row + 1

Loop

End Sub

在此处输入图片描述

相关内容