Copy and pasting using sendkeys with VBA

ZaxAlchemist

New Member
Joined
Feb 24, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm trying to create a Macro to use sendkeys command to copy the value of a cell and paste on another application.

It goes like this:

VBA Code:
Sub Inicio()

SendKeys "{F2}"
Application.Wait (Now + TimeValue("00:00:02"))
SendKeys "^a"
Application.Wait (Now + TimeValue("00:00:02"))
SendKeys "^c"
Application.Wait (Now + TimeValue("00:00:02"))
SendKeys ("%{TAB}")
Application.Wait (Now + TimeValue("00:00:01"))
SendKeys ("^v")
Application.Wait (Now + TimeValue("00:00:01"))
SendKeys ("~")

End Sub

I'm using the sequence F2 > ^a > ^c, because I need to copy the CONTENT of the cell. If I try using "selection.copy", the destination application won't accept the value.

The code runs, but it basically only executes the "%{TAB}" command and the "~" command, everything else gets "missing"....

Do I need to have any specific reference marked? Is there something that I'm doing wrong?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I'm using the sequence F2 > ^a > ^c, because I need to copy the CONTENT of the cell. If I try using "selection.copy", the destination application won't accept the value.

Just a clarification on this, I'm trying to copy a cell with 10 digits, when I use the "selection.copy" option, it copies 11 digits, because the "enter/return" digit is also copied for some reason, even if it doesn't even exist on the cell itself, and the destination application only accepts 10 digits....
 
Upvote 0
I suspect the problem is due to the clipboard. Its operation with sendkey is really uncertain.

If you move data out of Excel, you don't need a clipboard.

Cell or string can contain only characters which do not need function keys. (shift, alt, and so on)

You can use .value or string with sendkey
Dim x as String
x="something"


Application.SendKeys Cell.Value, True
Application.Wait (Now + TimeValue("0:00:01")
Application.SendKeys x, True
Application.Wait (Now + TimeValue("0:00:01")
Application.SendKeys ("{TAB}")
 
Upvote 0
Solution
Mr. Tupe77,

Thank you very much for your input, because it totally solved my issue.

I'll be forever in debt with you!
 
Upvote 0
hello, i have the same issue i need to copy excel cells in rows that form a simulation and copy them to another cell in a windows software for calculations. I was thinking of using sendkeys method but i have some problem with it. Can you help me
 
Upvote 0
What kind of problems? I won’t have time today, but I could try to check it tomorrow.

In general, however, you should start a new thread, so you usually get help faster than from old threads.
 
Upvote 0
basically, I have to take data from an excel file that contains mostly int or float that correspond to electrical engineering simulations and paste them into a software that is called cymcap to run these simulations. Each line of the excel file is a simulation and sometimes there are hundreds of lines with different parameters each time so for the moment we are forced to enter them by hand but it's not very nice hahaha. I have a script that will copy all the data from each cell of the excel file and paste them into the cymcap software.
2 big problems: 1- the cymcap software doesn't accept scripts so I can't add a script in the software and it doesn't accept the excel and txt files to upload them.
2- I thought of making a clickbot that could automate the copy and paste of each excel cell to the right cell in cymcap but it will just work for my computer because the resolution and screen size change (x and y position change hardcode).
I could add a mouse and keyboard recorder in the script so that the person copy paste the first line of excel with all the data and loop it for all the other lines but I'm blocked hahaha.
Or i can use the sendkey method but i am really not familliar to that.
Thank you so much for your help!
 
Upvote 0
just to add i also read that sendkeys is a very risky approach and its better to use API instead. But can API be implemented to my problem ?
 
Upvote 0
Send Key is problematic. You paste the data a blindly to active field, which is usually selected by sending a tab keystroke via sendkey to move from field to field.

I haven’t done anything like that through an api, but if a suitable api is available, it’s definitely a better way to accomplish that.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,985
Members
452,540
Latest member
haasro02

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