【发布时间】:2016-07-11 00:42:22
【问题描述】:
我正在尝试将 datagridview 值复制到 excel,到目前为止,我有这个代码可以工作,但只有线性
Sub Test()
Dim Wkb As Workbook
Set Wkb = ActiveWorkbook
notepadID = Shell("C:\Program Files\Default Company Name\TestInstaller\TestApp.exe")
SendKeys "admin", True
SendKeys "{TAB}", True
SendKeys "nothing", True
SendKeys "{ENTER}", True
SendKeys "{ENTER}", True
SendKeys "{LEFT}", True
SendKeys ("^C")
Application.Wait (Now + TimeValue("0:00:02"))
AppActivate "MicroSoft Excel"
Application.Wait (Now + TimeValue("0:00:02"))
ActiveCell.Offset(0, 0).Select
Application.Wait (Now + TimeValue("0:00:01"))
ActiveSheet.PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
AppActivate "Inventory"
SendKeys "{TAB}", True
SendKeys ("^C")
Application.Wait (Now + TimeValue("0:00:02"))
AppActivate "MicroSoft Excel"
Application.Wait (Now + TimeValue("0:00:02"))
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
End Sub
我想要做的是,从数据网格复制数据,数据网格由 4 行和 4 列组成,我想使用 vba sendkeys 将数据网格中的所有内容复制到我的 excel 中。提前谢谢!
编辑:
我知道有这段代码,但输出是对角线
Sub Test()
Dim Wkb As Workbook
Set Wkb = ActiveWorkbook
notepadID = Shell("C:\Program Files\Default Company Name\TestInstaller\TestApp.exe")
SendKeys "admin", True
SendKeys "{TAB}", True
SendKeys "nothing", True
SendKeys "{ENTER}", True
SendKeys "{ENTER}", True
Dim i As Integer
Dim j As Integer
i = 1
j = 1
SendKeys ("^C")
Application.Wait (Now + TimeValue("0:00:02"))
AppActivate "MicroSoft Excel"
Application.Wait (Now + TimeValue("0:00:02"))
ActiveCell.Offset(1, 1).Select
'Application.Wait (Now + TimeValue("0:00:01"))
ActiveSheet.PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
AppActivate "Inventory"
SendKeys "{TAB}", True
If Not IsEmpty(ActiveCell.Value) Then
Do While Cells(i, j).Value = Cells(i, j + 3).Value
j = j + 1
SendKeys ("^C")
Application.Wait (Now + TimeValue("0:00:02"))
AppActivate "MicroSoft Excel"
Application.Wait (Now + TimeValue("0:00:02"))
ActiveCell.Offset(i, j).Select
'Application.Wait (Now + TimeValue("0:00:01"))
ActiveSheet.PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
AppActivate "Inventory"
SendKeys "{TAB}", True
If i = 4 Then
i = i = 0
j = j + 1
End If
Loop
End If
End Sub
【问题讨论】:
-
我对 VB 和发送密钥的经验并不丰富,但你不能循环遍历单元格吗?例如
ActiveCell.Offsett(y, x).Select -
@BennoGrimm 先生,您能详细说明一下吗?我使用了偏移量,但参考不在单元格旁边,而是在对角线位置
-
你能解释一下你所说的它有效,但只有线性是什么意思吗?
-
在我程序的数据网格中,我有 4 列,我可以复制并粘贴第一行的所有内容。但在第二行,它往往在我的 Excel 工作表的第一行。先生,它也应该在我的工作表的第二行。