【问题标题】:Copying vb.net datagrid values to excel with vba sendkeys使用 vba sendkeys 将 vb.net 数据网格值复制到 excel
【发布时间】: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 工作表的第一行。先生,它也应该在我的工作表的第二行。

标签: excel vba datagrid


【解决方案1】:

所以不知何故,我能够在网上找到答案,我缺少的是循环。但不知何故,我无法动态识别我的 Datagridview 中有多少行,我只是放了一个静态数字。但到目前为止,这是我能得到的最接近的东西。

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(0, 0).Select

ActiveSheet.PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
AppActivate "Inventory"
SendKeys "{TAB}", True



If Not IsEmpty(ActiveCell.Value) Then

Do While Application.CountA(Range("A:A")) <> 13
    j = j + 1
    SendKeys ("^C")
    Application.Wait (Now + TimeValue("0:00:01"))
    AppActivate "MicroSoft Excel"
    Application.Wait (Now + TimeValue("0:00:01"))
    Cells(i, j).Select

    Application.Wait (Now + TimeValue("0:00:01"))
    ActiveSheet.Paste
    Application.Wait (Now + TimeValue("0:00:01"))
    AppActivate "Inventory"
    SendKeys "{TAB}", True

If j = 4 Then
    i = i + 1
    j = 0
End If

Loop
End If

【讨论】:

  • 因此,根据我阅读的许多帖子以及对其他论坛(如此线程)的回复,“您无法确定数据网格中的确切行数,因为 VBA 无法从任何应用程序到它的外壳,SendKeys 只能发送击键,仅此而已”。所以,我只是发送了 sendkeys 选项卡来复制数据网格中的所有文本,然后删除它要复制的最后一行,声明最后一行是静态的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多