【问题标题】:Excel VBA Subtracting one column from another, but not in a cellExcel VBA从另一列中减去一列,但不在单元格中
【发布时间】:2014-08-07 17:25:30
【问题描述】:
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的变量(这应该是一列),
不在工作表单元格上记录并引用单元格
,但这显然很难通过。

还有其他方法吗?

提前非常感谢!

【问题讨论】:

    标签: excel vba operator-keyword subtraction


    【解决方案1】:

    我认为您正在寻找 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
    

    【讨论】:

      【解决方案2】:

      这是一个基于您所描述的示例电子表格:

      这段代码将解决问题...为了证明它可以工作,最后有一个循环让消息框显示答案,但是你可以使用数组 如您计划的代码所示。我认为您可能需要考虑将数据存储在某个范围内的好处,但这是您的设计选择。

      Sub SubtractCells()
          Dim dblTolerance As Double
          Dim tmp As Range
          Dim A() As Double
          Dim intListSize As Integer
          Dim i As Integer
      
          'Get source range
          Set tmp = ActiveSheet.Range("C2")
      
          'Get size of list by finding last row
          Do Until tmp.Offset(1, 0).Value = ""
              Set tmp = tmp.Offset(1, 0)
          Loop
      
          'Subtract last row from first row of data to get number of rows and add one for offset from beginning
          intListSize = (tmp.Row - 2) + 1
      
          ReDim A(intListSize)
          i = 1 'index for array
      
          'use the temporary variable to cycle through the range
          Set tmp = ActiveSheet.Range("C2")
          Do Until tmp.Value = ""
              'Subtract the two rows and store in array
              A(i) = tmp.Value - tmp.Offset(0, 1).Value
      
              'Update range row and index
              i = i + 1
              Set tmp = tmp.Offset(1, 0)
          Loop
      
          'Loop through array to see results
          'This is only to verify code works comment out and use array
          'I would rethink storing this on a spreadsheet.
      
          For i = 1 To intListSize
              MsgBox ("A(" & Str(i) & "):" & Str(A(i)))
          Next
          'Clean up
          Set tmp = Nothing
      End Sub
      

      如果您有任何问题,请告诉我。

      【讨论】:

      • 您也可以使用 Debug.print 查看值,只需将 msgbox 更改为 debug.print 并删除括号,即 debug.print A(i) 如果您想让它更容易跨度>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多