|
- Option Explicit
- Sub Main()
- Dim shData As Worksheet, shCondition As Worksheet, shResult As Worksheet
- Dim arrData As Variant, arrCondition As Variant, objColID As Object
- Dim lngRow As Long, lngCol As Long, lngRow2 As Long
- Dim lngColID As Long, lngCurRow As Long
- Dim arrJudgeCondition As Variant, strName As String, strCountChar As String, strCountVal As String, strMaxChar As String, strMaxVal As String
- Dim arrResult As Variant, strTemp As String, lngCount As Long, dblMaxOrMin As Double, strRowID As String, blHasOK As Boolean
- Dim strSplit() As String, lngID As Long, arrTemp As Variant
-
- Set shData = Sheets("数据")
- Set shCondition = Sheets("分析条件")
- Set shResult = Sheets("分析结果")
- Set objColID = GetDicByColID
-
- arrData = shData.UsedRange '源数据
- arrCondition = shCondition.Range("A10:J18") '条件数据
- ReDim arrResult(1 To UBound(arrData), 1 To 26)
-
- For lngCol = 2 To UBound(arrCondition, 2)
- strName = arrCondition(1, lngCol)
- For lngRow = 2 To UBound(arrCondition) Step 4
- strCountChar = arrCondition(lngRow, lngCol)
- strCountVal = arrCondition(lngRow + 1, lngCol)
- strMaxChar = arrCondition(lngRow + 2, lngCol)
- strMaxVal = arrCondition(lngRow + 3, lngCol)
- '格式化条件
- arrJudgeCondition = GetJudgeCondition(strName, strCountChar, strCountVal, strMaxChar, strMaxVal)
- '如果条件列存在
- If objColID.Exists(arrJudgeCondition(0)) Then
- lngColID = objColID(arrJudgeCondition(0))
- '
- For lngRow2 = 2 To UBound(arrData)
- strTemp = arrData(lngRow2, lngColID)
- If Trim(strTemp) <> "" Then
- '符合条件开始计数
- If Application.Evaluate(strTemp & arrJudgeCondition(1)) = True Then
- blHasOK = True
- lngCount = lngCount + 1
- '峰值条件判断
- If Application.Evaluate(strTemp & arrJudgeCondition(2)) = True Then
- If Val(strTemp) > dblMaxOrMin Then
- dblMaxOrMin = Val(strTemp)
- strRowID = lngRow2
- ElseIf Val(strTemp) = dblMaxOrMin Then
- strRowID = strRowID & "|" & lngRow2
- End If
- End If
- Else
- If blHasOK And strRowID <> "" Then
- strSplit = Split(strRowID, "|")
- For lngID = LBound(strSplit) To UBound(strSplit)
- lngCurRow = lngCurRow + 1
- arrTemp = Application.WorksheetFunction.Index(arrData, Val(strSplit(lngID)), 0)
- PutDataToResult arrTemp, lngCurRow, lngCount * 0.25, lngColID, arrResult
- Next
- End If
-
- blHasOK = False
- lngCount = 0
- dblMaxOrMin = -99999
- strRowID = ""
- End If
- End If
- Next
- End If
- Next
- Next
-
- lngRow = shResult.UsedRange.Rows.Count
- If lngRow < 11 Then lngRow = 11
- shResult.Range("A11:Z" & lngRow).Clear
-
- shResult.Range("A11").Resize(lngCurRow, 26) = arrResult
-
- Set shData = Nothing
- Set shCondition = Nothing
- Set shResult = Nothing
- Set objColID = Nothing
-
- MsgBox "OK"
- End Sub
- Function PutDataToResult(arrData As Variant, lngRowID As Long, dblLength As Double, lngColID As Long, ByRef arrResult As Variant)
- Dim lngRow As Long
- For lngRow = 1 To 5
- arrResult(lngRowID, lngRow) = arrData(lngRow)
- Next
- For lngRow = 6 To 12
- arrResult(lngRowID, lngRow * 2 - 5) = arrData(lngRow)
- Next
- For lngRow = 13 To 16
- arrResult(lngRowID, lngRow + 7) = arrData(lngRow)
- Next
- arrResult(lngRowID, 25) = arrData(17)
-
- Select Case lngColID
- Case 17
- arrResult(lngRowID, 26) = dblLength
- Case 16
- arrResult(lngRowID, 24) = dblLength
- Case 6 To 11
- arrResult(lngRowID, lngColID * 2 - 4) = dblLength
- Case 5
- arrResult(lngRowID, 6) = dblLength
- End Select
- End Function
- Function GetDicByColID() As Object
- Dim objDic As Object, shData As Worksheet, arrData As Variant
- Dim strKey As String, lngCol As Long
-
- Set shData = Sheets("数据")
- arrData = shData.Range("A1:Q1")
- Set objDic = CreateObject("Scripting.Dictionary")
-
- For lngCol = 1 To UBound(arrData, 2)
- strKey = Trim(arrData(1, lngCol))
- objDic(strKey) = lngCol
- Next
- Set shData = Nothing
- Set GetDicByColID = objDic
- End Function
- Function GetJudgeCondition(strName As String, strCountChar As String, strCountVal As String, strMaxChar As String, strMaxVal As String) As Variant
- Dim strResult(2) As String, strTemp As String
-
- strResult(0) = Trim(strName)
- strResult(1) = myReplace(strCountChar, strCountVal)
- strResult(2) = myReplace(strMaxChar, strMaxVal)
- GetJudgeCondition = strResult
- End Function
- Function myReplace(strOld As String, strVal As String) As String
- Dim strReturn As String
- strReturn = strOld & Val(strVal)
- strReturn = Replace(strReturn, Space(1), "")
- strReturn = Replace(strReturn, "大于", ">")
- strReturn = Replace(strReturn, "小于", "<")
- strReturn = Replace(strReturn, "等于", "=")
- myReplace = strReturn
- End Function
复制代码 |
|