Help with SendKeys - only first SendKey command works

OddsAre

New Member
Joined
Apr 17, 2013
Messages
6
Howdy howdy,

I'm trying to paste Excel values into an external program (not an Office program). I usually have a list of maybe 20+ rows to enter, and find myself basically doing the following routine clicks/keys:

Choose the starting row in Excel
copy in excel
Alt-tab to focus on non-Office program
type "o"
tab
tab
tab
type "123"
tab
paste
tab
enter
Alt-Tab (to go back to Excel)
Select the next row and repeat

Basically:
I would like a script that starts with an excel cell active. I want to copy that cell, ALT TAB, TAB, enter "O", TAB, TAB, TAB, "123", TAB, PASTE, TAB, ENTER, ALT-TAB back to Excel, and go down one.

I have figured out how to get Excel to Alt-Tab, but can't for some reason get it to do anything after that:



Code:
Sub CopyWaitCopy()
Dim lastrow As Integer

lastrow = InputBox("What is the last row number of the entries?", "Stop!") 
' the above is there so my loop doesn't run forever

1:
 ActiveCell.Copy

Application.SendKeys "%{Tab}", True 'This works, I get it to alt-tab and put focus on the second program.
Application.SendKeys "{Tab}", True
Application.SendKeys "O", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True
Application.SendKeys "{Tab}", True

Application.SendKeys "123", True
Application.SendKeys "{Tab}", True

Application.SendKeys "^V", True
Application.SendKeys "{Tab}", True
'Application.SendKeys "{Enter}", True  ' -- is this how I would register the Enter key being pressed?



Application.SendKeys "%{Tab}", True


ActiveCell.Offset(1, 0).Select

If ActiveCell.row < lastrow Then
GoTo 1
Else
Exit Sub
End If
End Sub

I've read around online and apparently SendKeys isn't the most reliable. If there's no way to do what I'm looking to, that is fine. It's just a really repetitive task and I was hoping to automate with VBA.

Thank you very much for any insight/tips!

Ah, I have - Windows XP, Office 2010
 
Yes, that was me! ...

D'oh :p


Probably flakiness rearing it's ugly head unfortunately. Try increasing the wait and you may see a slight increase in reliability. No guarantees unfortunately, as universally observed send keys is a last resort.

Ah okay, that's what I was thinking.

Thanks so much Scott for your help! It's greatly appreciated. (y)
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,215,483
Messages
6,125,065
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top