VBA - CopyPasteCode with Sendkeys

廉价感情. 提交于 2020-01-07 05:05:13

问题


I'm using IDE in a excel workbook and trying to do the below code.

Sub cpypaste()    
    Range("E7").Select    
    SendKeys ("^c"), True    
    Application.Wait (Now + TimeValue("00:00:01"))    
    Range("G7").Select        
    SendKeys ("^v"), True    
End Sub

Not that I do not know alternate ways of doing it, but just curious why this is not working. I've tried running this code with keyboard shortcut and cmdbutton as well. Any help will be greatly appreciated.


回答1:


Using sendKeys is fecal at best. I assume you are starting the code from the code window, and because of this, the commands are trying to refer to that window. Excel needs to be activated to receive the commands.

Sub cpypaste()
AppActivate Application.Caption 'Activates the window.
Range("E7").Select

SendKeys String:="^c", Wait:=True

Application.Wait (Now + TimeValue("00:00:01"))' this line I belive is not needed, with the wait as true.
Range("G7").Select
SendKeys String:="^v", Wait:=True

End Sub


来源:https://stackoverflow.com/questions/33266859/vba-copypastecode-with-sendkeys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!