【问题标题】:Auto-filling a column with data once one cell has been updated一个单元格更新后自动用数据填充一列
【发布时间】:2016-01-15 23:44:39
【问题描述】:

如何在列中的一个单元格被填充后自动用 0 填充列?

例子:

原表

一旦我在四月列的任何单元格中输入了一个数字...

我希望该列自动用 0 填充其余单元格,就像这样...

我的第一个想法是像这样使用 Worksheet_Change 事件...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("H6:H16") 'H6 to H16 is the range of the April column, I would repeat this for each column in a loop

If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
    Range("H6:H16").Value = 0 
End If
End Sub

但这会覆盖整个列,而我想保留原始值。

Excel 是否有某种方法可以准确返回更改的单元格,以便我可以更改该列中除该列之外的所有单元格的值?还是有另一种更简单的方法来做到这一点?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    只是跳出框框思考可能的非 vba 方式来做这件事。您可以先用零填充所有单元格:

    然后从 A2 开始,突出显示“A2:L12”范围并转到首页-->条件格式-->新建规则-->使用公式确定要格式化的单元格和插入公式:

    =COUNTIF(A$2:A$12,">0")=0
    

    然后转到 Format-->Font 并将字体颜色更改为白色(另一种选择,可能更好的选择是将数字格式更改为自定义,类型为 @987654332 @ 由 Jeeped 建议)。

    按几次确定并退出条件格式规则管理器后,您的工作表应如下所示:

    向单元格添加大于零的值后,您将看到列中的所有零都出现。

    由于您输入的值必须大于零,因此您尝试执行的操作可能无法接受这种方法,但我想我会发布它以防您觉得可以使用它。

    【讨论】:

    • 使用类似方法时,我发现将数字格式更改为自定义类型:;;; 似乎比白色字体更不可见。
    • 不错!我更喜欢你的格式选项。我对我的答案进行了编辑
    • 虽然这可行,但它不适用于我正在做的事情。我有基于单元格中数据的图表,所以即使用户看不到零,图表仍然可以看到,并且会混淆格式。
    【解决方案2】:

    在 H6:H16 中使用 Apr,您的数据范围似乎在 E6:P16 中。

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("E6:P16")) Is Nothing Then
            On Error GoTo bm_Safe_exit
            Application.EnableEvents = False
            Dim tgt As Range, var As Variant
            For Each tgt In Intersect(Target, Range("E6:P16"))
                With Cells(6, tgt.Column).Resize(11, 1)
                    If Application.Count(.Cells) = 1 Then
                        var = tgt.Value
                        .Cells = 0
                        tgt = var
                    End If
                End With
            Next tgt
        End If
    bm_Safe_exit:
        Application.EnableEvents = True
    End Sub
    

    在将值写入工作表之前始终禁用事件处理,否则您将触发另一个在原始事件宏之上运行的 Worksheet_Change 事件宏,可能会触发一系列事件。

    【讨论】:

      【解决方案3】:

      这是我的回答,希望能给你一些帮助。

      Private Sub Worksheet_Change(ByVal Target As Range)
          Dim r
          Dim c
          Dim keyCells As Range
          Dim i
      
          r = Target.Row
          c = Target.Column
          'January in column E = 5
          'December in column P = 16
          Set keyCells = Range(Cells(6, c), Cells(16, c))
      
          If c >= 5 And c <= 16 Then ' if target is between the columns
              If r >= 6 And r <= 16 Then ' if the target is between the rows
                  For Each i In keyCells
                      If Not i.Address = Target.Address Then
                          Application.EnableEvents = False
                          i.Value = 0
                          Application.EnableEvents = True
                      End If
                  Next i
              End If
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2016-08-16
        • 1970-01-01
        • 2013-02-08
        • 2021-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多