ラブびあ

ビール。ときどきラブ

式を値で表示

Function 式(c As Range) As String
Dim s As String, a As String, i As Long, j As Long, r As Range

s = c.Formula
For i = 2 To Len(c.Formula) 'i:Midの開始位置 1文字目の「=」は飛ばす
For j = Len(c.Formula) - i + 1 To 1 Step -1 'j:Midの文字数
a = Mid(c.Formula, i, j)
On Error Resume Next
Set r = Nothing: Set r = Range(a)
On Error GoTo 0
If Not r Is Nothing Then
s = Replace(s, a, 値(r))
i = i + j
Exit For
End If
Next
Next

式 = s
End Function

Function 値(c As Range) As String
Dim s As String, d As String, r As Range
s = ""
d = ""

For Each r In c.Cells
If Len(s) > 0 Then d = ","

If r.HasFormula Then
s = s & d & "(" & 式(r) & ")"
Else
s = s & d & r.Value
End If
Next

If c.Cells.Count = 1 Then
値 = s
Else
値 = "(" & s & ")"
End If
End Function