|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
有一款身份证阅读器,读取身份证信息后可以分别使用 Ctrl+1、Ctrl+2、Ctrl+3、Ctrl+4键将姓名、性别、身份证号、地址这些信息快捷输入到excel的单元格中,于是我添加了一个按钮写了一段代码,点击按钮后自动将这些信息填入单元格中,代码如下:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const KEYEVENT_EXTENDEDKEY = &H1
Const KEYEVENT_KEYUP = &H2
Const VK_LCONTROL = &HA2 '左
Const VK_RCONTROL = &HA3 '右
Private Sub CommandButton1_Click()
Range("a1").Select
Call keybd_event(VK_LCONTROL, 0, 0, 0) '按下 左 control
Call keybd_event(vbKey1, 0, 0, 0) '按下 1 'Asc("1")
Call keybd_event(VK_LCONTROL, 0, KEYEVENT_KEYUP, 0) '松开 左 control
Call keybd_event(vbKey1, 0, KEYEVENT_KEYUP, 0) '松开 1
Range("a2").Select
Call keybd_event(VK_LCONTROL, 0, 0, 0)
Call keybd_event(vbKey2, 0, 0, 0)
Call keybd_event(VK_LCONTROL, 0, KEYEVENT_KEYUP, 0)
Call keybd_event(vbKey2, 0, KEYEVENT_KEYUP, 0)
Range("a3").Select
Call keybd_event(VK_LCONTROL, 0, 0, 0)
Call keybd_event(vbKey3, 0, 0, 0)
Call keybd_event(VK_LCONTROL, 0, KEYEVENT_KEYUP, 0)
Call keybd_event(vbKey3, 0, KEYEVENT_KEYUP, 0)
Range("a4").Select
Call keybd_event(VK_LCONTROL, 0, 0, 0)
Call keybd_event(vbKey4, 0, 0, 0)
Call keybd_event(VK_LCONTROL, 0, KEYEVENT_KEYUP, 0)
Call keybd_event(vbKey4, 0, KEYEVENT_KEYUP, 0)
End Sub
但是现在点击运行后信息全部输入到了最后一个“a4”单元格中了,不能填加到代码中的相应单元格中,不知道为什么。帮忙看一下代码有什么问题吗?
如果只有这一段代码是能正常填入的
Range("a1").Select
Call keybd_event(VK_LCONTROL, 0, 0, 0) '按下 左 control
Call keybd_event(vbKey1, 0, 0, 0) '按下 1 'Asc("1")
Call keybd_event(VK_LCONTROL, 0, KEYEVENT_KEYUP, 0) '松开 左 control
Call keybd_event(vbKey1, 0, KEYEVENT_KEYUP, 0) '松开 1
|
|