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/