excel - ActiveCell 更改然后在另一个单元格更改中添加
问题描述
请帮助我使用下面的宏,当我更改 activecell 中的值时,它无法正常工作。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim KeyCells As Range
kolumna = ActiveCell.Column
wiersz = ActiveCell.Row
komorka = Cells(wiersz, kolumna).Address
Set KeyCells = Range(komorka)
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Target.Offset(0, 1) = Target * 12
End If
Application.EnableEvents = True
End Sub
但是当我使用下面的代码时它可以工作,但是当我更改单元格中的值时它可以工作:$D$3
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim KeyCells As Range
Set KeyCells = Range("$D$3")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Target.Offset(0, 1) = Target * 12
End If
Application.EnableEvents = True
End Sub
所以我的代码的问题就在这里:
komorka = Cells(wiersz, kolumna).Address
非常感谢你的帮助
解决方案
返回后移动
MoveAfterReturn
如果设置为,它将不起作用True
,因为当您更改值时,光标会移动到下一个单元格,ActiveCell
因此它们永远不会相交(更正:如果您在列中A
并使用Left箭头确认条目,它仍然可以工作或在A1
使用Up箭头等)。- 如果您设置
MoveAfterReturn
为,它可以工作False
,所以我编写了切换它的过程。添加 aButton
或 aCommandButton
并在必要时使用它来启用或禁用它。 - 请注意,您必须使用 确认单元格条目Enter。箭头不会做。
表模块例如Sheet1
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.MoveAfterReturn Then
If Not Application.Intersect(ActiveCell, Target) Is Nothing Then
Dim cValue As Variant: cValue = Target.Value
If IsNumeric(cValue) Then
Target.Offset(0, 1).Value = CDbl(cValue) * 12
End If
End If
End If
End Sub
标准模块 例如Module1
Option Explicit
Sub toggleMAR()
With Application
If .MoveAfterReturn Then
.MoveAfterReturn = False
Else
.MoveAfterReturn = True
End If
End With
End Sub
推荐阅读
- javascript - 如何在Javascript中制作一个侧面有2个半圆的矩形按钮?
- amazon-web-services - 如何修复 EACCES:权限被拒绝,打开
- reactjs - 登录后 Firebase Firestore 身份验证令牌无限循环
- javascript - 如何定义带有变量的 onclick 函数?Javascript
- php - 如何在 php codeigniter 中获取聊天对话
- powershell - 如何使用powershell注册每个任务?
- angular - Angular项目未显示在浏览器上
- generics - “使 N 的形式参数类型适应 M 的类型参数”是否意味着特定于类型系统?
- xamarin.forms - 在代码中从侧面拉出导航抽屉
- firebase - Google Cloud Task 未调用 http 请求