vba excel:运行宏后取消选择范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44310845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:40:44  来源:igfitidea点击:

excel: deselect range after run macro

excelvbaexcel-vba

提问by paulinhax

I guess there is an easy way to solve this, but I couldn't see it clearly.

我想有一个简单的方法可以解决这个问题,但我看不清楚。

Everytime I change any cell in my in sheet it triggers this code below:

每次我更改工作表中的任何单元格时,它都会触发以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("A:S")) Is Nothing Then 
copy_column
End Sub

This code runs my copy_columnmacro (that copies some columns from my original sheet to another). That first code it happens because I want to automatically update the destination sheet when the origin sheet changes.

这段代码运行我的copy_column宏(将一些列从我的原始工作表复制到另一个)。第一个代码之所以发生是因为我想在原始工作表更改时自动更新目标工作表。

Sub copy_column()

Set origem = Sheets("FUNCIONáRIOS").Range("A4:C1040000")
Set destino = Sheets("BASE_TOTAL").Range("A2")
origem.Copy
destino.PasteSpecial Paste:=xlPasteValues

Set origem_subs = Sheets("FUNCIONáRIOS").Range("S4:S1040000")
Set destino_subs = Sheets("BASE_TOTAL").Range("J2")
origem_subs.Copy
destino_subs.PasteSpecial Paste:=xlPasteValues

Set origem_ini_fer = Sheets("FUNCIONáRIOS").Range("L4:L1040000")
Set destino_ini_fer = Sheets("BASE_TOTAL").Range("H2")
origem_ini_fer.Copy
destino_ini_fer.PasteSpecial Paste:=xlPasteValues

Set origem_fim_fer = Sheets("FUNCIONáRIOS").Range("P4:P1040000")
Set destino_fim_fer = Sheets("BASE_TOTAL").Range("I2")
origem_fim_fer.Copy
destino_fim_fer.PasteSpecial Paste:=xlPasteValues
End Sub

When my macro finishes it lets the last copied column selected like this:

当我的宏完成时,它让最后一个复制的列被选中,如下所示:

enter image description here

在此处输入图片说明

Is there any way to deselect this column at the end?

有没有办法在最后取消选择此列?

回答by Vityata

Press escape when you record the macro. It would give you:

录制宏时按 Esc 键。它会给你:

Application.CutCopyMode = False

Application.CutCopyMode = False

Edit: In general, your code does not need Copy and Paste - you only need the values. Thus, something like this will work:

编辑:通常,您的代码不需要复制和粘贴 - 您只需要值。因此,这样的事情会起作用:

Sub copy_column()

    Set origem = Sheets("FUNCIONáRIOS").Range("A4:C1040004")
    Set destino = Sheets("BASE_TOTAL").Range("A2:P1040002")
    destino.Value = origem.Value

    Set origem_subs = Sheets("FUNCIONáRIOS").Range("S4:S1040004")
    Set destino_subs = Sheets("BASE_TOTAL").Range("J2:J1040002")
    destino_subs.Value = origem_subs.Value

    Set origem_ini_fer = Sheets("FUNCIONáRIOS").Range("L4:L1040004")
    Set destino_ini_fer = Sheets("BASE_TOTAL").Range("H2:L1040002")
    destino_ini_fer.Value = origem_ini_fer.Value

    Set origem_fim_fer = Sheets("FUNCIONáRIOS").Range("P4:P1040004")
    Set destino_fim_fer = Sheets("BASE_TOTAL").Range("I2:P1040002")
    destino_fim_fer.Value = origem_fim_fer.Value

End Sub

And it is faster.

而且速度更快。