我经常使用 Excel 进行会计工作。我经常将多个值计算到同一个单元格中。所以我的单元格可能看起来像这样=12+345+23+88
。
现在,Excel 的默认行为是在按键时删除单元格的当前内容。如果你想添加一些文本,那么你必须双击或按 F2。
有没有办法将 Excel 设置为按键时附加?例如,在上面的例子中,我按下键+
,Excel 将打开单元格进行编辑,并将+
符号附加到末尾。
答案1
我以前也遇到过类似的问题,我找到了一个你可能也能用到的解决方案。以下内容经过修改以适合您的具体问题。
将其放入工作表的代码中:
Private Sub Worksheet_Activate()
Dim sht As Worksheet
Set sht = ActiveWorkbook.Worksheets("Sheet1")
If Not Intersect(ActiveCell, sht.Range("A1:A10")) Is Nothing Then
ChangeKeys True
End If
End Sub
Private Sub Worksheet_Deactivate()
ChangeKeys False
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim sht As Worksheet
Set sht = ActiveWorkbook.Worksheets("Sheet1")
If Not Intersect(ActiveCell, sht.Range("A1:A10")) Is Nothing Then
ChangeKeys True
Else
ChangeKeys False
End If
End Sub
将其放入模块中:
Sub ChangeKeys(KeysChanged As Boolean)
If KeysChanged Then
'Add procedures to the keys
'Functions on numpad (http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html)
Application.OnKey "{106}", "'KeyPress " & 42 & "'" 'Multiply *
Application.OnKey "{107}", "'KeyPress " & 43 & "'" 'Add +
Application.OnKey "{109}", "'KeyPress " & 45 & "'" 'Subtract -
Application.OnKey "{111}", "'KeyPress " & 47 & "'" 'Divide /
Else
'Reset the keys
'Functions on numpad (http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html)
Application.OnKey "{106}" 'Multiply *
Application.OnKey "{107}" 'Add +
Application.OnKey "{109}" 'Subtract -
Application.OnKey "{111}" 'Divide /
End If
End Sub
Function KeyPress(the_key As Long)
Application.SendKeys "{F2}{" & Chr$(the_key) & "}"
End Function
当您选择 Range("A1:A10") 中的单元格时,按键行为会针对“+ - * /”进行更改(尽管在 Excel 2013 中它似乎不适用于“/”),而当您选择此范围之外的单元格或激活另一张工作表时,按键行为会改回原样。当您停用/激活整个工作簿时,您可能希望执行类似操作。
此致,
西蒙