|
求任意三位数.rar
(36.2 KB, 下载次数: 3)
- Option Explicit
- Function CHONGHAO(rgSource As Range, intType As Integer, Optional intDisplay As Integer = 0, Optional varNum As Variant = "") As Variant
- Dim arrSource As Variant, arrResult As Variant, arrTemp As Variant, strNum As String
- Dim lngRows As Long, lngR As Long, strCur As String, strFirst As String
- Application.Volatile True
-
- lngRows = rgSource.Rows.Count
- strNum = Trim(varNum)
-
- If IsArray(rgSource) And lngRows > 1 Then
- arrSource = rgSource
- Else
- MsgBox "输入的数据源有误,请核对后重新输入!"
- Exit Function
- End If
-
- ReDim arrResult(1 To lngRows, 1 To 3) As String
-
- For lngR = 2 To UBound(arrSource)
- strCur = Trim(arrSource(lngR, 1))
- strFirst = Trim(arrSource(lngR - 1, 1))
- If strNum <> "" Then strFirst = strNum
- If strCur <> "" Then
- arrTemp = CheckStr(strFirst, strCur, intType)
- arrResult(lngR, 1) = arrTemp(1)
- arrResult(lngR, 2) = arrTemp(2)
- arrResult(lngR, 3) = arrTemp(3)
- End If
- Next
-
- Select Case intDisplay
- Case 0
- arrTemp = Application.WorksheetFunction.Index(arrResult, 0, 3)
- Case 1
- arrTemp = Application.WorksheetFunction.Index(arrResult, 0, 1)
- Case 2
- arrTemp = Application.WorksheetFunction.Index(arrResult, 0, 2)
- End Select
-
- CHONGHAO = arrTemp
- End Function
- Function CheckStr(strA As String, strB As String, Optional intType As Integer = 1) As Variant
- Dim lngID As Long, lngSum As Long
- Dim strCount As Long, strAddRess As String
- Dim strFind As String, strResult() As String
-
- ReDim strResult(1 To 3) As String
- If intType <> 1 Then intType = 2 '如果不是直选,设置为组选
-
- For lngID = 1 To 3
- Select Case intType
- Case 1 '直选算法
- lngSum = lngSum + Abs((Mid(strA, lngID, 1) = Mid(strB, lngID, 1)) * 1) * (2 ^ (lngID - 1))
- Case 2 '组选算法
- strFind = Mid(strA, lngID, 1)
- lngSum = lngSum + Abs((InStr(strB, strFind) > 0) * 1) * (2 ^ (lngID - 1))
- strB = Replace(strB, strFind, "-", 1, 1)
- End Select
- Next
-
- Select Case lngSum
- Case 0 '无重复
- strCount = 0: strAddRess = 0
- Case 1 '百位相同
- strCount = 1: strAddRess = 1
- Case 2 '十位相同
- strCount = 1: strAddRess = 2
- Case 3 '百位、十位相同
- strCount = 2: strAddRess = 1
- Case 4 '个位相同
- strCount = 1: strAddRess = 3
- Case 5 '百位、个位相同
- strCount = 2: strAddRess = 3
- Case 6 '十位、个位相同
- strCount = 2: strAddRess = 2
- Case 7 '三位都相同
- strCount = 3: strAddRess = 4
- End Select
-
- strResult(1) = strCount
- strResult(2) = strAddRess
- strResult(3) = strCount & strAddRess
-
- CheckStr = strResult
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|