0

これを正しくラベル付けしたかどうかわかりません。

データの文字列を含むセルがたくさんあります。各セルは次のようなもので構成されています。

q1 = 1   | q2 = 3.2 | q3 = 5.6
q1 = 1.8 | q3 = 2.1 | q5 = 1.4

*注意: 区切り文字はリテラルで、すべてのテキストは単一のセルにあり、パイプ文字が含まれています。

各セルをループし、(PHP 用語を使用するために) パイプ (|) 区切り記号で展開し、次に = 記号で再び展開します。

可能な値ごとに等号の左側に配列を作成し、右側にある値を配列に追加します (合計として追加するのではなく、配列に追加するように追加します)。

視覚的には、配列は次のようになるはずです。

Vars[
q1 [ 1,1.8 ],
q2 [ 3.2 ],
q3 [ 5.6,2.1]....] 

最終目標は、q1、q2、およびq3のそれぞれの平均、平均、および中央値を取得したいと考えています。

これはVBで実行できますか?私は PHP に精通していますが、これを Excel に保持したいと考えています。

ありがとう。

4

2 に答える 2

1

これにより、任意の数の「キー」(q1、q2など)が処理されます

Sub Tester()

'needs a reference to microsoft scripting runtime
Dim d As New Scripting.dictionary

Dim c As Range
Dim arrP, arrE
Dim q, v, tmpV, tmpP, tmpArr, uB
Dim i As Long, n As Long
Dim k

    For Each c In Selection.Cells
        tmpV = Trim(c.Value)

        If InStr(tmpV, "=") > 0 Then
            arrP = Split(tmpV, "|") 'split on pipe

            For i = LBound(arrP) To UBound(arrP)
                tmpP = arrP(i)

                If InStr(tmpP, "=") > 0 Then
                    q = Trim(Split(tmpP, "=")(0))
                    v = Trim(Split(tmpP, "=")(1))

                    If IsNumeric(v) Then
                        If Not d.exists(q) Then
                            d.Add q, Array(v)
                        Else
                            tmpArr = d(q) 'get dict value into temp array
                            uB = UBound(tmpArr) + 1
                            ReDim Preserve tmpArr(0 To uB) 'extend array
                            tmpArr(uB) = v
                            d(q) = tmpArr 'put back into dict
                        End If
                    End If
                End If
            Next

        End If 'cell has at least one "="

    Next c

    'dump the dictionary to the immediate pane
    For Each k In d.keys
        Debug.Print k, Join(d(k), ",")
    Next k


End Sub
于 2013-05-08T22:11:08.027 に答える
1

複雑ですが、実行できます。セル入力に基づいてExcelでこれをテストし、A1とA2に入れました:

q1 = 1   | q2 = 3.2 | q3 = 5.6
q1 = 1.8 | q3 = 2.1 | q5 = 1.4

2 つのループを使用して列 A のセルを循環し、「|」で分割する「ルーパー」というマクロを Excel にまとめました。各数値を検索し、それを double に変換して、対応する配列に配置します。

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

データを配列に正常に追加できたので、そこから平均などを計算するのは簡単なはずです。

于 2013-05-08T18:40:08.580 に答える