用于比较字符串数字是否在一列行中相等的公式
Formula to compare if string numbers are equal in one column row
我在 Excel 中有一个名为 "Positions" 的专栏。
该列可以有这样的字符串编号
职位
1-5
1-7
1-7
1-8
1-89
2-1
2-12
2-2
2-3
.......
N-N.. 第一个数字引用页码,第二个数字,即“-”之后引用页面位置。
页面从左到右分为9个位置,如下所示:
1 2 3
4 5 6
7 8 9
因此,当位置列中的数字 1-8 表示:
第 1 页
1 2 3
4 5 6
7 (8) 9
当位置列中有数字 2-12 时,表示:
第 2 页
(1) (2) 3
4 5 6
7 8 9
这就是它的设计方式,但是当我有一组如前所述的位置时,我想要修改的只是在位置列中
职位
1-5
1-7
1-7
1-8
1-89
2-1
2-12
2-2
2-3
.......
然后我需要一个公式以某种方式通知我位置 1-8 和 1-89 会重叠,位置 2-1、2-12 和 2-2 也会重叠。当然,位置 1-7 和 1-7 将完全重叠,因此这也应该通知用户。我该怎么做?
由于 OP 添加了 VBA
标签;请尝试此程序。
它在其对应的 3 pieces
中拆分 Position
,将它们与列表中的所有其他 Positions
进行比较。
它假定 Positions
列表从 B2
开始并在列 C
中列出比较结果。
'These Options declaration always go at the top of the module, class, etc.
Option Explicit
Option Base 1
Sub Get_Overlap()
Const kFlag As String = "Overlapping" 'Change as required
Dim rData As Range, aData As Variant, aResults() As String, sResult As String
Dim lA As Long, sAvalue As String, iAp As Integer, bA1 As Byte, bA2 As Byte
Dim lB As Long, sBvalue As String, iBp As Integer, bB1 As Byte, bB2 As Byte
Rem Sets Data Range & Arrays
With ThisWorkbook.Sheets("TEST").Columns("B") 'Change as required
Set rData = Range(.Cells(2), .Cells(Rows.Count).End(xlUp))
End With
aData = rData.Value2
aData = WorksheetFunction.Transpose(aData)
rData.Offset(0, 1).ClearContents
ReDim Preserve aResults(UBound(aData))
For lA = 1 To UBound(aData)
Rem Initialize & Set Item A Values
sAvalue = Empty: sAvalue = aData(lA)
iAp = 0: iAp = Left(sAvalue, 1)
bA1 = 0: bA1 = Mid(sAvalue, 3, 1)
On Error Resume Next
bA2 = 0: bA2 = Mid(sAvalue, 4, 1)
On Error GoTo 0
For lB = lA + 1 To UBound(aData)
Rem Initialize & Set Item B Values
sBvalue = Empty: sBvalue = aData(lB)
iBp = 0: iBp = Left(sBvalue, 1)
bB1 = 0: bB1 = Mid(sBvalue, 3, 1)
On Error Resume Next
bB2 = 0: bB2 = Mid(sBvalue, 4, 1)
On Error GoTo 0
Rem Initialize Comparison Result
sResult = Empty
Rem Compare Items & Values
Select Case True
Case sAvalue = sBvalue
sResult = kFlag
Case iAp = iBp
Select Case True
Case bA2 = 0 And bB2 = 0
If (bA1 = bB1) Then sResult = kFlag
Case bA2 = 0
If bA1 >= bB1 And bA1 <= bB2 Then sResult = kFlag
Case bB2 = 0
If bB1 >= bA1 And bB1 <= bA2 Then sResult = kFlag
Case Else
If bA1 >= bB1 And bA1 <= bB2 Then
sResult = kFlag
ElseIf bA2 >= bB1 And bA2 <= bB2 Then
sResult = kFlag
ElseIf bB1 >= bA1 And bB1 <= bA2 Then
sResult = kFlag
ElseIf bB2 >= bA1 And bB2 <= bA2 Then
sResult = kFlag
End If
End Select: End Select
Rem Add Results into Array
If sResult <> Empty Then
aResults(lA) = sResult
aResults(lB) = sResult
End If
Next: Next
Rem Enter Comparison Results
'Results will be posted one column to the right of where the List
'This is done by the use of "rData.Offset(0,1)"
rData.Offset(0, 1).Value = WorksheetFunction.Transpose(aResults)
End Sub
建议阅读以下页面以更深入地了解所使用的资源:
Option keyword,
Variables & Constants,
With Statement,
Range Object (Excel),
WorksheetFunction Object (Excel),
For...Next Statement,
Select Case Statement,
If...Then...Else Statement,
On Error Statement,
Range.Offset Property (Excel)
我在 Excel 中有一个名为 "Positions" 的专栏。 该列可以有这样的字符串编号
职位
1-5
1-7
1-7
1-8
1-89
2-1
2-12
2-2
2-3
....... N-N.. 第一个数字引用页码,第二个数字,即“-”之后引用页面位置。
页面从左到右分为9个位置,如下所示:
1 2 3
4 5 6
7 8 9
因此,当位置列中的数字 1-8 表示:
第 1 页
1 2 3
4 5 6
7 (8) 9
当位置列中有数字 2-12 时,表示:
第 2 页
(1) (2) 3
4 5 6
7 8 9
这就是它的设计方式,但是当我有一组如前所述的位置时,我想要修改的只是在位置列中
职位
1-5
1-7
1-7
1-8
1-89
2-1
2-12
2-2
2-3
.......
然后我需要一个公式以某种方式通知我位置 1-8 和 1-89 会重叠,位置 2-1、2-12 和 2-2 也会重叠。当然,位置 1-7 和 1-7 将完全重叠,因此这也应该通知用户。我该怎么做?
由于 OP 添加了 VBA
标签;请尝试此程序。
它在其对应的 3 pieces
中拆分 Position
,将它们与列表中的所有其他 Positions
进行比较。
它假定 Positions
列表从 B2
开始并在列 C
中列出比较结果。
'These Options declaration always go at the top of the module, class, etc.
Option Explicit
Option Base 1
Sub Get_Overlap()
Const kFlag As String = "Overlapping" 'Change as required
Dim rData As Range, aData As Variant, aResults() As String, sResult As String
Dim lA As Long, sAvalue As String, iAp As Integer, bA1 As Byte, bA2 As Byte
Dim lB As Long, sBvalue As String, iBp As Integer, bB1 As Byte, bB2 As Byte
Rem Sets Data Range & Arrays
With ThisWorkbook.Sheets("TEST").Columns("B") 'Change as required
Set rData = Range(.Cells(2), .Cells(Rows.Count).End(xlUp))
End With
aData = rData.Value2
aData = WorksheetFunction.Transpose(aData)
rData.Offset(0, 1).ClearContents
ReDim Preserve aResults(UBound(aData))
For lA = 1 To UBound(aData)
Rem Initialize & Set Item A Values
sAvalue = Empty: sAvalue = aData(lA)
iAp = 0: iAp = Left(sAvalue, 1)
bA1 = 0: bA1 = Mid(sAvalue, 3, 1)
On Error Resume Next
bA2 = 0: bA2 = Mid(sAvalue, 4, 1)
On Error GoTo 0
For lB = lA + 1 To UBound(aData)
Rem Initialize & Set Item B Values
sBvalue = Empty: sBvalue = aData(lB)
iBp = 0: iBp = Left(sBvalue, 1)
bB1 = 0: bB1 = Mid(sBvalue, 3, 1)
On Error Resume Next
bB2 = 0: bB2 = Mid(sBvalue, 4, 1)
On Error GoTo 0
Rem Initialize Comparison Result
sResult = Empty
Rem Compare Items & Values
Select Case True
Case sAvalue = sBvalue
sResult = kFlag
Case iAp = iBp
Select Case True
Case bA2 = 0 And bB2 = 0
If (bA1 = bB1) Then sResult = kFlag
Case bA2 = 0
If bA1 >= bB1 And bA1 <= bB2 Then sResult = kFlag
Case bB2 = 0
If bB1 >= bA1 And bB1 <= bA2 Then sResult = kFlag
Case Else
If bA1 >= bB1 And bA1 <= bB2 Then
sResult = kFlag
ElseIf bA2 >= bB1 And bA2 <= bB2 Then
sResult = kFlag
ElseIf bB1 >= bA1 And bB1 <= bA2 Then
sResult = kFlag
ElseIf bB2 >= bA1 And bB2 <= bA2 Then
sResult = kFlag
End If
End Select: End Select
Rem Add Results into Array
If sResult <> Empty Then
aResults(lA) = sResult
aResults(lB) = sResult
End If
Next: Next
Rem Enter Comparison Results
'Results will be posted one column to the right of where the List
'This is done by the use of "rData.Offset(0,1)"
rData.Offset(0, 1).Value = WorksheetFunction.Transpose(aResults)
End Sub
建议阅读以下页面以更深入地了解所使用的资源:
Option keyword, Variables & Constants, With Statement, Range Object (Excel), WorksheetFunction Object (Excel), For...Next Statement, Select Case Statement, If...Then...Else Statement, On Error Statement, Range.Offset Property (Excel)