如何防止excelclosures复制框

当用户select一个范围并将其复制到剪贴板时,通常会使用excel,您会注意到它会激活源范围内的选取框,有些人称这个animation为“行进中的ant”选框。 为了我的目的,我用键盘快捷键Ctrl-T创build了一个简单的VBA脚本,该脚本采用该剪贴板范围,并将其值和格式粘贴到新select的目标范围中。 (跳过它的备忘录和这样的属性,我不需要粘贴到新的范围)。

如果我想添加到VBA脚本,并且还有权alignment目标范围文本,则会出现问题。 问题出在我将代码添加到右alignment后,它将closures选取框并禁用源范围的剪贴板内容,以便将来的任何Ctrl-T(我的macros)热键按下更多的目标范围。 如果我没有在VBA的末尾添加额外的代码,那么我可以继续在源表单上粘贴源代码范围到许多目的地范围,而不必重新select我想要粘贴的原始源代码范围/单元格电子表格。

简单的解决scheme是重新select目标范围并将其复制到剪贴板中,但由于在我的情况下,源select是单个单元,而目标单元将会是所有不同的范围,所以这不起作用。

这是有问题的代码:

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False With Selection .HorizontalAlignment = xlRight End With 

谢谢你的帮助。

好吧,我不认为我会很快得到答案,因为有人指出,Excel不允许你获得原始的范围坐标。 是的,我相信这是真实的,我们不能做那么容易的事情,但作为程序员,我们必须做出应有的承诺。 这是解决我的问题,我希望它可以帮助其他人:

只需更改复制键盘快捷键Ctrl-C的默认特征,并将其replace为执行标准副本的自己的子例程,但也可将对象保存到全局variables中。 这样,我们的自定义键盘热键顺序可以根据需要随时访问,无论是否有子例程closures选取框并清除剪贴板内容。

在同一个VBA模块,我有复制和粘贴特殊的SUBRoutines我创build了2个全局variables,包含Source&Destination对象。

这是工作代码:

 Public SourceCell As Range Public DestinationCell As Range Sub MyCopy() ' ' MyCopy Macro (Make sure to set the macro shortcut option to Ctrl-C) ' Set SourceCell = Selection Selection.Copy End Sub Sub MyPaste() ' ' MyPaste Macro (Make sure to set the macro shortcut option to Ctrl-T or our choice) ' Set DestinationCell = Selection SourceCell.Select Selection.Copy DestinationCell.Select 'You can substitute your own paste code here Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False With Selection .HorizontalAlignment = xlRight End With 'The two lines below are not necessary for our custom paste hotkey but It: '1) Recreates the marquee for standard Ctrl-V paste operations '2) Shows you what your original selection was in case you forgot SourceCell.Select Selection.Copy 'Then add the following line so the screen doesn't shift back to source cell DestinationCell.Select End Sub 

所以基本上上面的VBA模块添加将允许您使用Ctrl-C复制一个范围和Ctrl-T来粘贴值和格式,然后它会自动alignment。 请记住,您要粘贴的过程是用户可修改的。

感谢stackoverflow让我离开我的笔记…