这很复杂,但可以做到。我根据您的单元格输入在 excel 中对此进行了测试,将它们放在 A1 和 A2 中:
q1 = 1 | q2 = 3.2 | q3 = 5.6
q1 = 1.8 | q3 = 2.1 | q5 = 1.4
我在 Excel 中放置了一个名为“Looper”的宏,它使用两个循环循环遍历 A 列中的单元格,将它们拆分为“|” 并搜索每个数字值,将其转换为双精度并将其放入相应的数组中。
Private Sub Looper()
''Loop Variables
Dim i, k As Integer
Dim MoveDown As String
''Variables to manipulate the string
Dim Selecter As String
Dim TotalCell As String
Dim Splitter As Variant
Dim strIncrement As String
''Array variables and counters
Dim q1(50) As Double
Dim q2(50) As Double
Dim q3(50) As Double
Dim qv1, qv2, qv3 As Integer
''Variables for finding the number in each increment
Dim Equals As Integer
Dim strNumber As String
Dim dblNumber As Double
''Set the array counters to 0
qv1 = 0
qv2 = 0
qv3 = 0
i = 0
Do Until MoveDown = "DONE"
Selector = "A" + Replace(Str(i), " ", "")
If Range(Selector).Value = "" Then
MoveDown = "DONE"
Else
TotalCell = Range(Selector).Value
Splitter = Split(TotalCell, "|")
For k = LBound(Splitter) To UBound(Splitter)
''strIncrement holds the data in between each |
strIncrement = Splitter(k)
''Remove any spaces
strIncrement = Replace(strIncrement, " ", "")
''Equals shows the location of the number (length of string - loc of =)
Equals = Len(strIncrement) - InStr(1, strIncrement, "=")
strNumber = Right(strIncrement, Equals)
dblNumber = CDbl(strNumber)
''Check for the array name and then add the data to the corresponding array
If InStr(1, strIncrement, "q1") > 0 Then
q1(qv1) = dblNumber
qv1 = qv1 + 1
Else
If InStr(1, strIncrement, "q2") > 0 Then
q2(qv2) = dblNumber
qv2 = qv2 + 1
Else
If InStr(1, strIncrement, "q3") > 0 Then
q3(qv3) = dblNumber
qv3 = qv3 + 1
End If
End If
End If
Next
End If
i = i + 1
Loop
End Sub
我能够成功地将数据添加到数组中,因此从那里开始计算平均值等应该很简单。