從 az AZ 0-9 按下的每個鍵都會啟動一個宏LetterPress,并以按下的鍵作為引數呼叫。基本上我Application.OnKey每個鍵都有一個 az AZ 0-9
如果活動單元格是某個單元格,我想啟動一個子單元,否則我只想像往常一樣發送密鑰。這開始了一個沒有結束的遞回回圈。這是原始代碼:
Sub LetterPress(B as String)
If Application.ActiveCell.Row = 3 And Application.ActiveCell.Column = 5 And Application.ActiveSheet.Name = "Search" And Not IsNumeric(B) Then
Call Search1(B)
Else
Application.SendKeys B 'This line starts a recursive loop
End if
我嘗試重置OnKey,發送密鑰,然后將密鑰分配回宏。但是最后一行(分配位)仍然開始一個遞回回圈。
Sub LetterPress(B as String)
If Application.ActiveCell.Row = 3 And Application.ActiveCell.Column = 5 And Application.ActiveSheet.Name = "Search" And Not IsNumeric(B) Then
Call Search1(B)
Else
Application.OnKey B 'Reset Application.OnKey so a recursive loop doesn't start. This line works.
Application.SendKeys B 'Send the string B (a letter). This works too.
Application.OnKey B, "'LetterPress """ & B & """'" 'Assign whatever is in string variable B back to the
End if 'macro, with it self as the parameter.
'This line somehow triggers B and starts the
'recursive loop.
有誰知道為什么?
uj5u.com熱心網友回復:
你的嘗試還不錯。問題在于Application.SendKeys B,您正在發送密鑰,但未對其進行處理。首先,您的代碼會一直執行到最后,然后 Excel 會重新獲得控制權并可以處理擊鍵。那時,您的OnKey-definition 已經處于活動狀態。
但是,有一個簡單的解決方案:讓 Excel 有機會完成這項作業。您可以使用DoEvents.
Application.OnKey B
Application.SendKeys B
DoEvents
Application.OnKey B, "'LetterPress """ & B & """'"
我做了一個測驗,至少它對我有用——我很好奇你能否證實。
uj5u.com熱心網友回復:
這可能有效:
Sub LetterPress(B as String)
If Application.ActiveCell.Row = 3 And Application.ActiveCell.Column = 5 And Application.ActiveSheet.Name = "Search" And Not IsNumeric(B) Then
Call Search1(B)
Else
Application.OnKey B 'Reset Application.OnKey so a recursive loop doesn't start. This line works.
Application.SendKeys B 'Send the string B (a letter). This works too.
Application.DoEvents
Application.OnKey B, "'LetterPress """ & B & """'" 'Assign whatever is in string variable B back to the
End if 'macro, with it self as the parameter.
'This line somehow triggers B and starts the
'recursive loop.
話雖如此; 我從未真正嘗試過使用DoEvents()函式。讓我知道它是否有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/433525.html
上一篇:將多個作業表匯出為CSV
