式置換が優れている


1

Excelで式置換を簡単に行うことは可能ですか?

下記の例のようなワークシートを持っています(もっと複雑ですが、もしあるのであればexcelで何らかの機能が必要です)。

私がやりたいことは、式ではなく実際の入力値で総売上関数を置き換えることです。

F6+I6+L6+O6 = F9 + L9
F4+F5+I4+I5+L4+L5+O4+O5 = F6+I6+L6+O6

that is 
F9 + L9 becomes F4+F5+I4+I5+L4+L5+O4+O5

enter image description here

私が何をしているのか理解してください


でお試しください webapps.stackexchange.com
aparente001

1
Webアプリケーションではありません。そしてafaik、Excelの質問はトピックに
Viktor Mellgren

そうそう。私は混乱します、Excelは一方のサイトに行き、Google Sheetsはもう一方に行きます。
aparente001

入力した方法ではなく、式ですべての先例を参照したいですか。
Raystafarian

可能であれば@Raystafarian、まさに根本的な先例に、私は繰り返しそれを行うことができれば一歩下がっても大丈夫
Viktor Mellgren

回答:


2

私はあなたが望むことをするべきである簡単なコードを書きました。

基本的にセルに数式が含まれているかどうかをチェックし、含まれている場合はそのセルがその数式で参照されているすべてのインスタンスを置き換えます。

一度それがシートを通過したならば、それは再びループする(それが実際にそれがこれを必要とすると確信していないが入れることがより簡単であった)。また、これが複雑なスプレッドシートでどれだけ速く実行されるかもわかりません。

$ A $ 1、$ A1、A $ 1、およびA1は、参照に固定されたセルがあるかどうかを判断する方法がないため、同じものとして扱われます。

Sub replace_formulas()
Dim cell_count As Long, flag As Boolean

Do

flag = False

For Each c In ActiveSheet.UsedRange
    If c.HasFormula Then

        'count number of replacements
        cell_count = Application.WorksheetFunction.CountIf(Cells, c.Address) + _
            Application.WorksheetFunction.CountIf(Cells, Replace(c.Address, "$", "")) + _
            Application.WorksheetFunction.CountIf(Cells, Replace(c.Address, "$", "", 1, 1)) + _
            Application.WorksheetFunction.CountIf(Cells, "$" & Replace(c.Address, "$", ""))

        'If there is at least one replacement loop through all the cells after this one
        If cell_count > 0 Then flag = True

        'Replace cell references with and without $ ($A$1,$A1,A$1,A1)
        Cells.Replace What:=c.Address, Replacement:="c.formula", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        Cells.Replace What:=Replace(c.Address, "$", ""), Replacement:=Right(c.Formula, Len(c.Formula) - 1), LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        Cells.Replace What:=Replace(c.Address, "$", "", 1, 1), Replacement:=Right(c.Formula, Len(c.Formula) - 1), LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
        Cells.Replace What:="$" & Replace(c.Address, "$", ""), Replacement:=Right(c.Formula, Len(c.Formula) - 1), LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    End If
Next

Loop While flag = True

End Sub

1

手動で実行する必要があるかもしれませんが、役立つ機能があります。先例をトレースする(Excel 2013の[式]タブ)

これにより、選択した式の元になる場所を示す矢印が追加されます。したがって、例のJ列で総売上を選択してトレースの先例をクリックすると、F9とL9から矢印が引かれます。


私はその機能を知っていますが、私は最終的な式で約600セルになる(そしておそらく15レベルの深い式で)大きなスプレッドシートを持っています。
Viktor Mellgren
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.