Excel 中用公式的值替换公式本身

在 Excel 中,公式或公式的一部分都可以用它得出来的值来替换。当然啦,被替换的部分就不再是公式了,不会再跟着引用值的改变而改变了。
Excel 的帮助文件里讲了两种方法,但是最好用的那招就是F9,会这招就可以通吃了。鉴于上篇日志提交后成果泡汤的前车之鉴,这篇的内容继续保持英文。

Replace a formula with its calculated value

Caution  When you replace a formula with its value, Microsoft Excel permanently removes the formula. If you accidentally replace a formula with a value and want to restore the formula, click Undo Button image immediately after you enter or paste the value.

  1. Select the cell that contains the formula.

    If the formula is an array formula (A formula that performs multiple calculations on one or more sets of values, and then returns either a single result or multiple results. Array formulas are enclosed between braces { } and are entered by pressing CTRL+SHIFT+ENTER.), select the range that contains the array formula.

    How?

    1. Click a cell in the array formula.
    2. On the Edit menu, click Go To.
    3. Click Special.
    4. Click Current array.
  2. Click Copy Button image.
  3. Click Paste Button image on the Standard toolbar (A bar with buttons and options that you use to carry out commands. To display a toolbar, click Customize on the Tools menu, and then click the Toolbars tab.)
  4. Click the arrow next to Paste Options Button image, and then click Values Only.

Replace part of a formula with its calculated value

Caution  When you replace part of a formula with its value, it cannot be restored.

  1. Click the cell that contains the formula.
  2. In the formula bar (A bar at the top of the Excel window that you use to enter or edit values or formulas in cells or charts. Displays the constant value or formula stored in the active cell.)   Formula bar, select the portion of the formula you want to replace with its calculated value. When you select the part of the formula you want to replace, make sure to include the entire operand (Items on either side of an operator in a formula. In Excel, operands can be values, cell references, names, labels, and functions.) For example, if you select a function, you must select the entire function name, the opening parenthesis, the arguments (The values that a function uses to perform operations or calculations. The type of argument a function uses is specific to the function. Common arguments that are used within functions include numbers, text, cell references, and names.), and the closing parenthesis.
  3. To calculate the selected portion, press F9.
  4. To replace the selected portion of the formula with its calculated value, press ENTER.

    If the formula is an array formula, press CTRL+SHIFT+ENTER.

注,仅仅选择单元格后按F9是没用的,要双击进入单元格的编辑模式后按F9,就可以用生成的值替换公式本身。
你可以使用 Python 的 `openpyxl` 库来读取和写入 Excel 文件,并使用字符串的 `replace()` 方法来替换单元格中的内容。下面是一个示例代码,演示了如何实现你的需求: ```python from openpyxl import load_workbook def replace_in_excel(filename, sheetname, search_str, replace_str): # 加载 Excel 文件 wb = load_workbook(filename) sheet = wb[sheetname] # 遍历单元格,替换内容 for row in sheet.iter_rows(values_only=True): for cell in row: if isinstance(cell, str): original_value = cell new_value = original_value.replace(search_str, replace_str) cell.value = f"{original_value} -> {new_value}" # 保存修改后的 Excel 文件 wb.save(f"modified_{filename}") # 示例用法 replace_in_excel("example.xlsx", "Sheet1", "old", "new") ``` 这个示例中,我们首先加载 Excel 文件,然后遍历每个单元格,如果单元格的是字符串类型,则使用 `replace()` 方法替换指定的字符。替换后,在单元格中使用 `->` 符号将原文和替换后的内容连接起来。最后,保存修改后的 Excel 文件。 请确保你已经安装了 `openpyxl` 库,可以使用 `pip install openpyxl` 命令进行安装。同时,将代码中的 `example.xlsx` 替换为你实际的 Excel 文件路径,`Sheet1` 替换为你想要操作的工作表名称,`old` 替换为你要查找的字符,`new` 替换为你要替换的字符。 希望这个示例能帮助到你!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值