2016-07-11 31 views
0

我試圖複製datagridview的值出類拔萃脫穎而出,到目前爲止,我有這個代碼的工作,但只有線性複製vb.net數據網格值與VBA的SendKeys

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 

我願意這樣做是,從數據網格複製數據,datagrid由4行和4列組成,我想從datagrid使用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 
+0

我對VB並沒有真正的經驗和發送鍵,但你不能只是循環槽?例如'ActiveCell.Offsett(y,x).Select' –

+0

@BennoGrimm先生,您能否詳細說明一下?我使用偏移量,但參考不在單元格旁邊,而是在對角線位置 – UserSeriously

+0

您能否通過_It的作用解釋您的意思,但僅限於linear_? –

回答

0

所以不知何故,我能在網上找到答案,我錯過了什麼是循環。但不知何故,我無法動態識別在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 
+0

因此,根據我已閱讀的許多帖子以及對此線程等其他論壇的回覆,「您無法確定數據網格中的確切行數,因爲VBA無法將任何應用程序的對象繼承到其外殼,SendKeys只能發送擊鍵,沒有更多「。所以,我只是發送sendkeys選項卡來複制datagrid中的所有文本,然後刪除它將複製的最後一行,並聲明最後一行是靜態的。 – UserSeriously