在一定数量的列和行上运行程序

时间:2014-05-07 08:15:07

标签: excel excel-vba vba

让程序按照我的意愿运行的问题,我已经设法让孔列工作但我需要所有12行来处理行星星在20这样“T2”并跳过下一列22这么“V2”......一直到“AP2”。 该程序的要点是在输入栏中输入库存并将其与当前库存一起添加,然后输入栏变为空。

   Private Sub worksheet_change(ByVal target As Range)
     If target.Column = 20 And target.Value > 0 Then
     Dim val As Double
   val = target.Value
     target.Value = 0
        Cells(target.Row, target.Column - 1).Value = val + Cells(target.Row, target.Column - 1).Value
   End If
  End Sub

1 个答案:

答案 0 :(得分:0)

有趣的方法。试试这个......

区域/单个单元格的区域/列的逐步处理将支持一步复制+粘贴多个值,一步复制从一个单元格复制到其他几个单元格。

Application.EnableEvents语句可防止事件触发自身。

祝你好运! : - )

Private Sub worksheet_change(ByVal target As Range)
    Dim rngArea As Range, rngColumn As Range, rngSingleCell As Range
    Dim lngColumn As Long
    Dim dblVal As Double
    Application.EnableEvents = False
    'loop though all changed areas
    For Each rngArea In target
        'loop though all changed columns of this area
        For Each rngColumn In rngArea
            'check if column number shall be processed, i.e. is 20 / 22 / 24 ... 42
            lngColumn = rngColumn.Column
            If ((lngColumn >= 20) And (lngColumn <= 42) And ((lngColumn Mod 2) = 0)) Then
                'loop though all changed cells of this column of this area
                For Each rngSingleCell In rngColumn
                    If rngSingleCell.Value > 0 Then
                        dblVal = rngSingleCell.Value
                        rngSingleCell.Value = 0
                        Cells(rngSingleCell.Row, rngSingleCell.Column - 1).Value = dblVal + Cells(rngSingleCell.Row, rngSingleCell.Column - 1).Value
                        End If
                    Next rngSingleCell
                End If
            Next rngColumn
        Next rngArea
    Application.EnableEvents = True
    End Sub

此致 Kawi42