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?
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

ZaxAlchemist

New Member
Joined
Feb 24, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
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....
 

Tupe77

Board Regular
Joined
Nov 26, 2020
Messages
78
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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}")
 
Solution

ZaxAlchemist

New Member
Joined
Feb 24, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Mr. Tupe77,

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

I'll be forever in debt with you!
 

Watch MrExcel Video

Forum statistics

Threads
1,127,199
Messages
5,623,321
Members
415,966
Latest member
ctorohuamanchumo

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
Top