gpt4 book ai didi

excel - 使用鼠标按钮更改单元格值 - Excel

转载 作者:行者123 更新时间:2023-12-04 20:44:05 24 4
gpt4 key购买 nike

我正在尝试使用鼠标按钮更改单元格的值。
我想做的是:

如果您左键单击某个范围的单元格:F3 到 F500 和 G3 到 G500。并且单元格中有一个数值。比方说7。加1。所以如果我左键单击值为7的F4。值变为8。如果我右键单击该单元格,它会减去。单元格 (7) 的值变为 6。

我对 VBA 很陌生,我什至不知道如何开始。所以我的问题是:我该怎么做?如果这个问题不够具体。如何注册这些单元格上的鼠标点击?

最佳答案

所以,经过一番尝试,我得到了这段代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim Intersection
Dim Rng As Range
Set Rng = Range(Cells(3, 6), Cells(500, 7))

Set Intersection = Application.Intersect(Target, Rng)
If Not Intersection Is Nothing Then
If IsNumeric(Selection.Value) And Selection.Value <> "" Then
If (GetAsyncKeyState(vbKeyRButton)) Then 'right mouse button
Selection.Value = (Selection.Value - 1)
ElseIf (GetAsyncKeyState(vbKeyLButton)) Then 'left mouse button
Selection.Value = (Selection.Value + 1)
End If
Cells(Selection.Row, 1).Select
End If
End If

End Sub

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
End Sub

您必须将此代码粘贴到工作表 的 代码部分(很可能是 Visual Basic View 中“Microsoft Excel 对象”下的“Sheet1”)。它不适用于其他模块,但特定工作表之一。您还必须将以下代码添加到常规模块(很可能是“Module1”):
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

它可能会帮助您了解此代码的作用: SelectionChange 始终在工作表中的选择时调用,左键单击、右键单击、箭头按钮等之间没有区别。键状态功能允许我们专门检查左键或右键单击.

选定的单元格总是切换到左侧的同一行,否则 Excel 将无法检测到另一个左键单击(左键单击没有特定事件,并且同一单元格不会再次触发 SelectionChange ) .

关于excel - 使用鼠标按钮更改单元格值 - Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23564019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com