繁体   English   中英

将除法公式应用于Excel宏中的整个列?

[英]Apply divide formula to entire column in macro on Excel?

我不仅要将此公式应用于B2单元格,而且要应用于整个B列。

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target = [B2] Then
    Dim amount As Long
    amount = [B2]

    Application.EnableEvents = False
    Target.Value = amount / (100000000)
    Application.EnableEvents = True
  End If
End Sub

任何帮助将不胜感激。

像这样:

Private Sub Worksheet_Change(ByVal Target As Range)

  Dim rng As Range, c As Range, rngCol As Range

  'Get the range from the table column header
  Set rngCol = Me.ListObjects("myTable").ListColumns("Col2").DataBodyRange

  'EDIT: alternative for multiple contiguous columns
  Set rngCol = Me.Range("myTable[Col2]:myTable[Col4]")

  'any changes in the monitored column?
  Set rng = Application.Intersect(Target, rngCol)

  If Not rng Is Nothing Then '<< got some changes in ColB
      Application.EnableEvents = False
      For Each c In rng.Cells
          If IsNumeric(c.Value) Then 
              'Update: only apply to values >1
              If c.Value > 1 Then c.Value = c.Value / 100000000
          End If
      Next c
      Application.EnableEvents = True
  End If

End Sub

(已编辑以显示对Table / ListObject的使用)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM