Excel で数式内の参照を評価することは可能ですが、数式の構造を維持することはできますか? たとえば、A1 = 5 および B1 = 10 の場合
=(A1+B1)/B1
になるだろう
=(5+10)/10
ただし、1.5 に評価されるのではなく、すべての数式構造 (つまり = + /) を保持します。
Excel で数式内の参照を評価することは可能ですが、数式の構造を維持することはできますか? たとえば、A1 = 5 および B1 = 10 の場合
=(A1+B1)/B1
になるだろう
=(5+10)/10
ただし、1.5 に評価されるのではなく、すべての数式構造 (つまり = + /) を保持します。
私はあなたがいじることができるいくつかのほとんど動作するコードを持っています。個々のセルが数式に含まれている限り機能します-したがって、機能しませんA1:C1
が、機能しA1,B1,C1
ます-VLOOKUPSまたはMATCHはありません。単純な式だけ。
コードの主要部分は Bill Manville の功績によるものです。
Stackoverflow リンクがあります: Address of first layer of precedent cells via VBA in Excel and
元のコードへのリンク:
http://www.vbaexpress.com/forum/showthread.php?19348-Solved-Splitting-all-addresses-in-a-formula&p=142863#post142863
http://www. ozgrid.com/forum/showthread.php?t=17028
コードは、セル内のコメントに値を含む式を配置します。
Public Sub ReplaceWithValues()
' From original code written by Bill Manville for FindPrecedents.
Dim rLast As Range, iLinkNum As Integer, iArrowNum As Integer
Dim bNewArrow As Boolean
Dim sTmpFormula As String
ActiveCell.ShowPrecedents
Set rLast = ActiveCell
iArrowNum = 1
iLinkNum = 1
bNewArrow = True
'Get an absolute reference version of the formula.
sTmpFormula = Application.ConvertFormula _
(Formula:=ActiveCell.Formula, _
fromReferenceStyle:=xlA1, _
toReferenceStyle:=xlA1, _
toAbsolute:=xlAbsolute)
Do
Do
Application.Goto rLast
On Error Resume Next
ActiveCell.NavigateArrow TowardPrecedent:=True, ArrowNumber:=iArrowNum, LinkNumber:=iLinkNum
On Error GoTo 0
If rLast.Address(external:=True) = ActiveCell.Address(external:=True) Then Exit Do
bNewArrow = False
If rLast.Worksheet.Parent.Name = ActiveCell.Worksheet.Parent.Name Then
'Formula precedent is in same workbook.
If rLast.Worksheet.Name = ActiveCell.Parent.Name Then
'Formula precedent is on the same sheet as formula.
sTmpFormula = Replace(sTmpFormula, Selection.Address(external:=True), Selection.Value)
sTmpFormula = Replace(sTmpFormula, Selection.Address, Selection.Value)
Else
'Formula precedent is in same workbook, but different sheet.
If InStr(Selection.Parent.Name, " ") > 0 Then
'If the sheet name contains a space the reference will have ' at either end.
sTmpFormula = Replace(sTmpFormula, "'[" & Selection.Parent.Parent.Name & "]" & _
Selection.Parent.Name & "'!" & Selection.Address, Selection.Value)
Else
sTmpFormula = Replace(sTmpFormula, "[" & Selection.Parent.Parent.Name & "]" & _
Selection.Parent.Name & "!" & Selection.Address, Selection.Value)
End If
End If
Else
sTmpFormula = Replace(sTmpFormula, Selection.Address(external:=True), Evaluate(Selection.Address(external:=True)))
End If
iLinkNum = iLinkNum + 1 ' try another link
Loop
If bNewArrow Then Exit Do
iLinkNum = 1
bNewArrow = True
iArrowNum = iArrowNum + 1 'try another arrow
Loop
rLast.Parent.ClearArrows
Application.Goto rLast
rLast.AddComment
rLast.Comment.Text Text:=sTmpFormula
Application.ScreenUpdating = True
End Sub