vba 插入行而不选择任何内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2714831/
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-08 10:24:34  来源:igfitidea点击:

Inserting Rows Without Selecting Anything?

excelvbaexcel-vba

提问by sooprise

I'm working in VBA and want to insert a row in a specific location without selecting it. The issue I'm having is that after the row is selected, the spreadsheet is scrolled down to that row when the script is finished running. I want to be able to do this without the spreadsheet being scrolled down to the inserted row.

我在 VBA 中工作,想在特定位置插入一行而不选择它。我遇到的问题是,在选择行后,当脚本运行完成时,电子表格会向下滚动到该行。我希望能够在不将电子表格向下滚动到插入的行的情况下执行此操作。

Rows(i & ":" & i).Select
ActiveCell.EntireRow.Insert

I don't want to select A1 to get to the top.

我不想选择 A1 来登顶。

回答by BradC

Just do this:

只需这样做:

Cells(i,1).EntireRow.Insert

Apply the action directly to the desired range, instead of selecting it first and applying the action to "Activecell".

将动作直接应用于所需范围,而不是先选择它并将动作应用于“Activecell”。

A few additional notes:

一些额外的注意事项:

  • The "record a macro" feature generates code like your example, but I find that selecting the cell in advance is seldom necessary when writing your own code.
  • A side benefit: operating on the cell directly instead of selecting it first can speed up your code by a factor of 10x to 100x !!
  • “记录宏”功能会生成类似于您的示例的代码,但我发现在编写自己的代码时很少需要提前选择单元格。
  • 一个附带好处:直接在单元格上操作而不是先选择它可以将您的代码速度提高 10 到 100 倍!!

回答by Alex K.

Rows(i & ":" & i).Insertwill insert a row below without changing the selection.

Rows(i & ":" & i).Insert将在下面插入一行而不更改选择。