求助:如何修正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 (likeB10instead 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
相关产品推荐
相关产品推荐

