英文:
Excel VBA Disabling Shortcut Keys Sometimes Disables Data Entry
问题
The provided code is meant to disable certain keyboard shortcuts and key combinations in Excel. It should not affect simple data entry. However, you mentioned that it occasionally prevents you from entering data into cells. This issue is occurring randomly, and you've observed that it persists even after entering edit mode by double-clicking the cell.
Closing and reopening the file temporarily solves the problem, but it returns occasionally.
It's important to note that without access to the entire context and Excel environment, it can be challenging to pinpoint the exact cause of this issue. You may want to check for any conflicting macros or add-ins that could interfere with Excel's default behavior. Additionally, you might consider revising the code or seeking assistance on Excel-related forums or communities for a more in-depth analysis of the problem.
If you have any specific questions or need further assistance, please let me know.
英文:
Why is my Code disabling even data entry randomly and occasionally, but working most of the time even when I isolate it and remove all other macros?
The code disables pretty much all shortcut keys, but should not affect simple data entry.
Option Explicit
Rem mod__ShortCutKeys
' Ctrl = "^"
' Alt = "%"
' Shift = "+"
' Combinations ("^", "%", "+", "^%", "^+", "%+", "^%+")
Rem Declare Variables
Dim Ctrl As String
Dim Alt As String
Dim Shift As String
Dim CtrlAlt As String
Dim CtrlShift As String
Dim AltShift As String
Dim CtrlAltShift As String
Dim KeyArray As Variant
Dim Key As Variant
Dim i As Long
Rem Public Sub ShortCutKeysInitializeVariables
Public Sub ShortCutKeysInitializeVariables()
Ctrl = "^"
Alt = "%"
Shift = "+"
CtrlAlt = "^%"
CtrlShift = "^+"
AltShift = "%+"
CtrlAltShift = "^%+"
KeyArray = Array(Ctrl, Alt, Shift, CtrlAlt, CtrlShift, AltShift, CtrlAltShift)
End Sub
Rem Public Sub ShortCutKeysDisable
Sub ShortCutKeysDisable()
Rem Calls
Call ShortCutKeysInitializeVariables
' Combinations
For Each Key In KeyArray
For i = 1 To 255
On Error Resume Next
Application.OnKey Key & Chr(i), ""
Next i
Next Key
' Functions keys
For i = 1 To 12
On Error Resume Next
Application.OnKey "{F" & i & "}", ""
Next i
' Combinations & Functions keys
For Each Key In KeyArray
For i = 1 To 12
On Error Resume Next
Application.OnKey Key & "{F" & i & "}", ""
Next i
Next Key
End Sub
Inspired by: Disable key or key combination
Why would this code stop me from entering any data? Happens randomly and only sometimes.
What I mean by data entry is that if I have a cell selected and I try to type data in that cell (uppercase or lowercase), it doesn't do anything. That problem persists even if I double-click in the cell to enter edit mode.
Closing and opening the file solves the problem, but it returns occasionally.
I'm using: Excel 2021 Pro Plus LTSC
答案1
得分: 1
如评论中所述,Application.Onkey
在编辑模式下不应对按键行为产生影响,这告诉我这可能是Excel/VBA的一个bug。
这可能是因为您设置了大量的OnKey
目标,导致一些处理按键的过程表现不稳定,甚至有时会挂起。
尝试的一个方法是减少您设置的按键组合的数量。您尝试在第一个循环中设置了1,785(7 x 255)个OnKey
目标!
如果您的目标是定位键盘快捷键,您可以缩小字符范围,从32到255,因为我真的怀疑您的键盘是否能够输出不可打印的ASCII字符。您甚至可以考虑将其缩小到32到127,这取决于您的键盘是否具有带重音的字符。
如果这不足以解决问题,您可以尝试将Application.OnKey
组合重置为其原始状态,然后重新禁用所有快捷键,以查看是否可以正确重置事物。
例如,您可以有一个修改版的过程,利用了将Application.OnKey "...", Null
传递的事实将键的行为重置为原始状态:
Sub ShortCutKeysToggle(Enabled As Boolean)
Dim Proc As Variant
If Enabled Then
Proc = Null
Else
Proc = ""
End If
ShortCutKeysInitializeVariables
' Combinations
For Each Key In KeyArray
For i = 32 To 255
On Error Resume Next
Application.OnKey Key & Chr(i), Proc
Next i
Next Key
' Functions keys
For i = 1 To 12
On Error Resume Next
Application.OnKey "{F" & i & "}", Proc
Next i
' Combinations & Functions keys
For Each Key In KeyArray
For i = 1 To 12
On Error Resume Next
Application.OnKey Key & "{F" & i & "}", Proc
Next i
Next Key
End Sub
然后,当您遇到问题时,可以有以下过程来切换或重置键:
Sub ShortCutKeysEnable()
ShortCutKeysToggle True
End Sub
Sub ShortCutKeysDisable()
ShortCutKeysToggle False
End Sub
Sub ShortCutKeysReset()
ShortCutKeysToggle True
ShortCutKeysToggle False
End Sub
更新: 以下部分现在不相关,因为解决问题的方法不是关闭和重新打开文件,而是关闭和重新打开Excel应用程序。
如果这不起作用,您提到关闭并重新打开文件可以解决问题,所以您可以创建一个宏来关闭文件并重新打开它:
Sub CloseAndReopenActiveWorkbook()
Application.ScreenUpdating = False
Dim FileName As String
FileName = ActiveWorkbook.FullName
ActiveWorkbook.Close SaveChanges:=True
Workbooks.Open FileName:=FileName
Application.ScreenUpdating = True
End Sub
英文:
As stated in the comments, Application.Onkey
should not have any impact on the behavior of key presses when you are in Edit mode, so this tells me that it's likely a bug with Excel/VBA.
It could possibly be due to the fact that you are setting a lot of OnKey
targets and it causes some processes that handles key presses to behave erratically and even hang sometimes.
One thing to try would be to reduce the numbers of key combinations you set. You are trying to set 1,785 (7 x 255) OnKey
targets in the first loop only!
If your goal is to target keyboard shortcuts, you can certainly reduce the range of characters from 32 to 255 since I really doubt that your keyboard can output non-printable ascii characters. You might even consider reducing it to 32 to 127 depending if you have accented characters on your keyboard.
If this isn't enought to fix it, you can try to reset the Application.OnKey
combinations to their original state and re-do the disabling of all shortcuts to see if that resets things properly.
For instance, you could have a modified version of your procedure that makes use of the fact that passing Application.OnKey "...", Null
will reset the behavior of the keys that were passed:
Sub ShortCutKeysToggle(Enabled As Boolean)
Dim Proc As Variant
If Enabled Then
Proc = Null
Else
Proc = ""
End If
ShortCutKeysInitializeVariables
' Combinations
For Each Key In KeyArray
For i = 32 To 255
On Error Resume Next
Application.OnKey Key & Chr(i), Proc
Next i
Next Key
' Functions keys
For i = 1 To 12
On Error Resume Next
Application.OnKey "{F" & i & "}", Proc
Next i
' Combinations & Functions keys
For Each Key In KeyArray
For i = 1 To 12
On Error Resume Next
Application.OnKey Key & "{F" & i & "}", Proc
Next i
Next Key
End Sub
Then, you could have the following procedures to toggle or reset the keys when you get the bug that you have.
Sub ShortCutKeysEnable()
ShortCutKeysToggle True
End Sub
Sub ShortCutKeysDisable()
ShortCutKeysToggle False
End Sub
Sub ShortCutKeysReset()
ShortCutKeysToggle True
ShortCutKeysToggle False
End Sub
UPDATE: The following section is now irrelevant because it's not closing and reopening the file that solves the problem, but closing and reopening the Excel application.
And if that doesn't work, you mentioned that closing and opening the file solved the problem, so you could always just have a macro that closes the file and reopen it.
Sub CloseAndReopenActiveWorkbook()
Application.ScreenUpdating = False
Dim FileName As String
FileName = ActiveWorkbook.FullName
ActiveWorkbook.Close SaveChanges:=True
Workbooks.Open FileName:=FileName
Application.ScreenUpdating = True
End Sub
答案2
得分: 0
Sure, here are the translated parts:
- 确保代码放在适当的模块中并正确执行。请再次检查您是否从代码或Excel工作簿的适当位置调用了ShortCutKeysDisable过程。
- 检查Excel工作簿中是否存在任何冲突的代码或宏。如果有其他宏或插件会操作快捷键,它们可能会干扰ShortCutKeysDisable过程的行为。
- 检查代码是否存在错误或不一致之处。确保语法正确,所有变量和对象都已正确声明和使用。检查代码中是否存在拼写错误或缺失字符。
- 考虑在新的、空白的Excel工作簿中测试代码,以隔离与当前工作簿中现有设置或配置可能存在的冲突。
- 如果问题仍然存在,请尝试在不同的计算机或Excel安装上运行代码,以查看问题是否特定于您的环境。
英文:
- Make sure the code is placed in the appropriate module and is being
executed correctly. Double-check that you are calling the
ShortCutKeysDisable procedure from the appropriate location in your
code or Excel workbook. - Check for any conflicting code or macros in your Excel workbook. If
there are other macros or add-ins that manipulate shortcut keys,
they might interfere with the behavior of the ShortCutKeysDisable
procedure. - Review the code for any errors or inconsistencies. Make sure that
the syntax is correct and all variables and objects are properly
declared and used. Check for any typos or missing characters in the
code. - Consider testing the code in a new, blank Excel workbook to isolate
any potential conflicts with existing settings or configurations in
your current workbook. - If the issue persists, try running the code on a different computer
or Excel installation to see if the problem is specific to your
environment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论