Excel VBA从另一列中减去一列,但不在单元格中

标签 excel vba operator-keyword subtraction

Sub TryAgain()
    Dim A As Range '~ I will put the result in this column, A
    Dim B As Range
    Dim C As Range '~ [B-C] is the what I need
    Dim onecell As Range
    Dim twocell As Range
    Set B = Range("C2", Range("C2").End(xlDown)) 'log prices of Z over time
    Set C = Range("D2", Range("D2").End(xlDown)) 'log prices of WPC over time
    Set A(1) = B.Value - C.Value
End Sub

我一直在努力解决这个恼人的问题。

这是两列之间的简单减法运算,

所以我想做的是进行“CELL-BY-CELL”减法运算

但是!我不想将结果放入单元格(待引用)或创建任何其他工作表。

所以,我想将减法结果(即 C2-D2、C3-D3 ...)直接分配给名为 A 的变量(这应该是一列),
不记录工作表单元格并引用单元格
,但这显然很难完成。

还有其他方法可以做到这一点吗?

提前非常感谢!

最佳答案

我认为您正在寻找 Array :

Sub TryAgain()
Dim A() As Integer '~ I will put the result in this column, A
Dim i As Integer
Dim lastrow As Integer

lastrow = Range("B2").End(xlDown).Row '<- assume there's no empty cells from C2 to last row on column C
ReDim A(lastrow)

For i = 1 To lastrow
A(i) = Cells(i + 1, 2).Value - Cells(i + 1, 3).Value
Next
' add below for loop to print out result
For j = 1 To lastrow - 1
Debug.Print A(j)
Next

End Sub

关于Excel VBA从另一列中减去一列,但不在单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188581/

相关文章:

vba - 使用 VBA 循环不同的单元格范围以在每个单元格范围中编写公式

python - 如何访问已建立的许多行中的一行

excel - 在 Excel 中根据条目创建自动日期戳

excel - VBA 使用相同的宏按钮展开/折叠行

不同文件之间二维矩阵的c++下标运算符

c++ - 懒惰、重载的 C++ && 运算符?

vba - excel vba - 在自动筛选后选择除标题之外的所有筛选行

excel - 计算 SMA 时处理空白单元格

excel - 使用 Excel 求解器生成多个最优解

c++ - 为什么这段代码会产生错误?