1

セルから数式文字列をパラメーターとして取得し、含まれるすべてのセルを文字列配列として返す関数を作成しようとしています。

Function GetCells(str As String) As String
    Dim stringArray() As String

    GetCells = stringArray
End Function

セル内のリンクされたすべてのセルを通過し、セル名を文字列に置き換える再帰関数でこれを使用したいと考えています。これは概念コードの一部です:

Dim result As String
Dim cell As Range
Dim stringArray() As String
Dim arraySize As Integer

Set stringArray = GetCells("A1 + A2")
arraySize = UBound(stringArray)

For n = 0 To arraySize Step 1
   Set cell = Range(stringArray(n))
   result = Replace(result, stringArray(n), "Some text")
Next

これに対する私の唯一の解決策は、ステート マシンを作成し、文字と整数のペアを探して、結果から配列を構築することです。いくつかの機能を通してこれを行う簡単な方法はありますか? はいの場合、どのように?

4

3 に答える 3

2

あなたが探しているのはこれだと思います:

Range("A1").Precedents.Address

( Range.Precedents プロパティ)

したがって、A1 に次の式があるとします。

=B1+C2-D3

次にRange("A1").Precedents.Address戻ります:

$B$1,$C$2,$D$3

式が次の場合:

=INDEX($D$1:$D$17,1,1)

その後$D$1:$D$17、返されます。

これをどのように使用できますか?評価する範囲の関数に Range オブジェクトを渡すだけで、返されたアドレスのリストを取得し、それを別の範囲オブジェクトにスローて、各セルを評価します。

例を次に示します (セル A1 と A2 に数式が含まれているとします)。

Option Explicit

Public Function getCells(ByRef r As Excel.Range) As String
    Dim s As String

    getCells = r.Precedents.Address
End Function

Public Sub test()
    Dim rangeString As String
    Dim r As Excel.Range
    Dim cell As Excel.Range

    rangeString = getCells(Sheet1.Range("A1:A2"))
    Set r = Range(rangeString)

    For Each cell In r
        ' do stuff
        Debug.Print "hello: " & cell.Address(0, 0)
    Next cell
End Sub
于 2013-03-06T00:40:05.387 に答える
0

あなたが答えに投稿した解決策をうまく機能させようとして失敗した後、私は自分自身のものを作成しました。
ステートマシンを作成することで問題が解決し、1x1セルで完全に機能すると思ったので、これが私が望んでいたすべてです。

ソースコード

Function isChar(char As String) As Boolean
    Select Case char
        Case "A" To "Z"
            isChar = True
        Case Else
            isChar = False
    End Select
End Function

Function isNumber(char As String, isZero As Boolean) As Boolean
    Select Case char
        Case "0"
            If isZero = True Then
                isNumber = True
            Else
                isNumber = False
            End If
        Case "1" To "9"
            isNumber = True
        Case Else
            isNumber = False
    End Select
End Function

Function GetCells(str As String) As String
    Dim stringArray() As String
    Dim stringSize As Integer 'size of stringArray
    Dim c As Integer 'character number
    Dim chr As String 'current character
    Dim tempcell As String 'suspected cell's temporaly result
    Dim state As Integer 'state machine's state:
    '0 - nothing
    '1 - 1 character eg. A from A1
    '2 - 2 character eg. AG from AG156
    '3 - 3 character eg. AGH from AGH516516
    '4 - characters with number(s) eg. AH15 from AH1569
    '5 - first dollar sing eg. $ from $A$1
    '6 - second sollar sing eg. $A$ from $A$1

    Dim testresult As String

    state = 0
    stringSize = 0

    For c = 0 To Len(str) Step 1
        chr = Mid(str, c + 1, 1)
        Select Case state
            Case 0
                If isChar(chr) Then
                    state = 1
                    tempcell = tempcell & chr
                ElseIf chr = "$" Then
                    state = 5
                    tempcell = tempcell & chr
                Else
                    state = 0
                    tempcell = ""
                End If
            Case 1
                If isNumber(chr, False) Then
                    state = 4
                    tempcell = tempcell & chr
                ElseIf isChar(chr) Then
                    state = 2
                    tempcell = tempcell & chr
                ElseIf chr = "$" Then
                    state = 6
                    tempcell = tempcell & chr
                Else
                    state = 0
                    tempcell = ""
                End If
            Case 2
                If isNumber(chr, False) Then
                    state = 4
                    tempcell = tempcell + chr
                ElseIf isChar(chr) Then
                    state = 3
                    tempcell = tempcell + chr
                ElseIf chr = "$" Then
                    state = 6
                    tempcell = tempcell + chr
                Else
                    state = 0
                    tempcell = ""
                End If
            Case 3
                If isNumber(chr, False) Then
                    state = 4
                    tempcell = tempcell + chr
                ElseIf chr = "$" Then
                    state = 6
                    tempcell = tempcell + chr
                Else
                    state = 0
                    tempcell = ""
                End If
            Case 4
                If isNumber(chr, True) Then
                    state = 4
                    tempcell = tempcell + chr
                Else
                    state = 0
                    stringSize = stringSize + 1
                    ReDim Preserve stringArray(stringSize)
                    stringArray(stringSize - 1) = tempcell
                    tempcell = ""
                End If
            Case 5
                If isChar(chr) Then
                    state = 1
                    tempcell = tempcell + chr
                Else
                    state = 0
                    tempcell = ""
                End If
            Case 6
                If isNumber(chr, False) Then
                    state = 4
                    tempcell = tempcell + chr
                Else
                    state = 0
                    tempcell = ""
                End If
            Case Else
                state = 0
                tempcell = ""
        End Select
    Next c
    'GetCells = stringArray
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'This part is only for easily print the string array
    For c = 0 To stringSize Step 1
        testresult = testresult + " | " + stringArray(c)
    Next
    GetCells = testresult
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Function

Sub Main()
Dim s As String
s = "A1+B1+$A1-$B$65"
MsgBox (GetCells(s))

s = "(A5*2+HJ$15)-((F5+F1)-$F11+$PP$659)"
MsgBox (GetCells(s))

'also some crazy input
s = "A$61+$HK2+'p0thecakeisalie/0p'+DDD5+D1-$B$12-LCK$5065"
MsgBox (GetCells(s))

End Sub

テスト

テストを作成して、実際の動作を確認できるようにしました。最初の2つは日常の使用をシミュレートしており、3つ目はクレイジーな入力ですが、アルゴリズムは引き続き機能します。

ケース1

  • 入力:A1+B1+$A1-$B$65
  • 出力:| A1 | B1 | $A1 | $B$65 |

ケース2

  • 入力:(A5*2+HJ$15)-((F5+F1)-$F11+$PP$659)
  • 出力:| A5 | HJ$15 | F5 | F1 | $F11 | $PP$659 |

ケース3

  • 入力:A$61+$HK2+'p0thecakeisalie/0p'+DDD5+D1-$B$12-LCK$5065
  • 出力:| A$61 | $HK2 | DDD5 | D1 | $B$12 | LCK$5065 |
于 2013-03-08T16:37:27.677 に答える