|
- Sub Test()
- Dim arrSource As Variant, arrResult As Variant
- Dim lngRow As Long, lngCount As Long, intDiff As Integer, lngID As Long
- Dim strTemp As String, strSplit() As String, strFind As String
-
- arrSource = Sheet1.Range("A2:A10")
- arrResult = Sheet1.Range("A13:B21")
-
- strFind = Sheet1.Range("A12")
-
- For lngRow = LBound(arrSource) To UBound(arrSource)
- strTemp = arrSource(lngRow, 1)
- strTemp = Trim(strTemp)
- strSplit = Split(strTemp, " ")
- For lngID = LBound(strSplit) To UBound(strSplit)
- intDiff = GetDiff(strSplit(lngID))
- If InStr(strFind, intDiff) < 1 Then
- strSplit(lngID) = ""
- End If
- Next
- strTemp = Join(strSplit, " ")
- Do Until InStr(strTemp, " ") < 1
- strTemp = Replace(strTemp, " ", " ")
- Loop
- strTemp = Trim(strTemp)
- arrResult(lngRow, 1) = strTemp
- arrResult(lngRow, 2) = UBound(Split(strTemp, " ")) + 1
- Next
-
- Sheet1.Range("A13:B21") = arrResult
- End Sub
- Function GetDiff(strTemp As String) As Integer
- Dim intArr() As Integer
- Dim intIndex As Integer
-
- strTemp = Trim(strTemp)
- intIndex = Len(strTemp)
- ReDim intArr(1 To intIndex) As Integer
-
- For intIndex = 1 To 3
- intArr(intIndex) = Mid(strTemp, intIndex, 1)
- Next
-
- intIndex = Application.WorksheetFunction.Max(intArr) - Application.WorksheetFunction.Min(intArr)
- GetDiff = intIndex
- End Function
复制代码 |
|