You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何修正VBA代码以实现单元格减法公式的正确写入?

Fixing VBA Formula for Cell Subtraction

Hey there! Let's get that subtraction formula working correctly for you.

First, let's break down what was off with your original code:

  • You wrapped the cell addresses in unnecessary single quotes (''), which would turn them into text values instead of valid cell references.
  • The Sum() function is unnecessary here—you just need a simple subtraction between two cells, no summing required.
  • The extra parentheses around each cell reference were adding unnecessary complexity and breaking the formula structure.

Corrected Code

Here's the adjusted VBA code that will generate the exact =BX-BY formula you're looking for:

With Worksheets(c10) 
    .Cells(NewEngineRowNumber + 3, 2).Formula = "=" & .Cells(NewEngineRowNumber + 6, 2).Address(0, 0) & "-" & .Cells(NewEngineRowNumber + 2, 2).Address(0, 0)
End With

How It Works

  • Address(0, 0) generates a relative cell reference (like B10 instead of $B$10), which matches your desired output.
  • We directly concatenate the equals sign, the first cell address, the minus operator, and the second cell address—no extra functions or quotes needed.

This will populate the target cell with a formula like =B10-B6 (depending on your NewEngineRowNumber value), exactly as you wanted.

内容的提问来源于stack exchange,提问作者CptGoodar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:34:44