[英]Excel VBA - Copy and Paste each cell Individually to another program
I'm fairly new to VBA but know my way around it a bit (sorta).我对 VBA 还很陌生,但对它有点了解(有点)。 What I'm needing to do is get a Macro set to copy data in one cell, switch to another program (2315), enter "6", [ENTER], paste the data, [ENTER] switch back to Excel, select the next row, copy, switch, paste, switch, until it reaches the end of the column.
我需要做的是获取一个宏集来复制一个单元格中的数据,切换到另一个程序(2315),输入“6”,[ENTER],粘贴数据,[ENTER]切换回Excel,select下一行,复制,切换,粘贴,切换,直到它到达列的末尾。 This is what I have, which works for the first cell.
这就是我所拥有的,适用于第一个单元格。 I just need to figure out how to loop it until it reaches the end.
我只需要弄清楚如何循环它直到它到达终点。 Thanks for any help/suggestions!
感谢您的任何帮助/建议!
Range("A2").Select
Selection.Copy
AppActivate "2315"
Application.SendKeys "6~^V~"
Application.SendKeys "%{TAB}"
Thanks Again!再次感谢!
I doubt that this works, but you say it does and your question is about the loop.我怀疑这行得通,但你说它行得通,你的问题是关于循环的。 Here is the loop.
这是循环。
Dim R As Long
For R = 2 To Cells(Rows.Count, "A").End(xlUp).Row
Cells(R, "A").Copy
AppActivate "2315"
Application.SendKeys "6~^V~"
Application.SendKeys "%{TAB}"
Next R
For Excel's sake you don't need to select anything.为了 Excel,您不需要 select 任何东西。 The above code will loop through all the used cells in column A of the active tab.
上面的代码将遍历活动选项卡的 A 列中所有使用的单元格。 If your app "2315" needs the selection, add
Cells(R, "A").Select
before the copy instruction.如果您的应用程序“2315”需要选择,请在复制指令之前添加
Cells(R, "A").Select
。
My doubt is based in the fact that you can't use VBA to send parameters to app "2315".我的怀疑是基于您不能使用 VBA 将参数发送到应用程序“2315”的事实。 This includes the instruction to "2315" to surrender control back to Excel and its VBA.
这包括指令“2315”将控制权交还给 Excel 及其 VBA。 Perhaps you have found such a way via the SendKeys.
也许您已经通过 SendKeys 找到了这种方法。 If so, the code will work.
如果是这样,代码将起作用。 If not, it will get stuck after the first loop.
如果不是,它将在第一个循环后卡住。
Logically, the code should run until AppActivate "2315", meaning the SendKeys are never sent until the other app has returned control.从逻辑上讲,代码应该一直运行到 AppActivate “2315”,这意味着在其他应用程序返回控制权之前永远不会发送 SendKeys。 If that is what's going on there must be better ways to express whatever your two instructions get done.
如果这是正在发生的事情,那么必须有更好的方式来表达你完成的两条指令。 But that shouldn't be your immediate concern.
但这不应该是你的直接关注。 If you can get "2315" to surrender control back to Excel with a simple command like Enter , and if the above macro really resumes work where it left off, most of your problem should be solved.
如果您可以使用Enter之类的简单命令让“2315”将控制权交还给 Excel,并且如果上述宏真的从中断的地方恢复工作,那么您的大部分问题都应该得到解决。 This I hope for:-)
我希望这个:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.