vba 将一行插入到一组特定的列

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

Insert a Row to a Specific Set of Columns

excelexcel-vbarowvba

提问by Ryan B

I'm working on a spreadsheet to act as a master copy for some important information that will be kept track of for a project. On the main sheet I have a table for notes and a table for contacts set up separated by several columns and some VB code to insert new notes or new contacts. However, when I insert a new note I want it to appear at the top of the table and to move all existing notes down a row. Is there any way to add a row to a specific set of columns so that any other data that is kept on cells with the same row, but in a different table, are not moved?

我正在制作一个电子表格,作为一些重要信息的主副本,这些信息将被跟踪以用于项目。在主表上,我有一个笔记表和一个联系人表,由几列和一些 VB 代码分隔,用于插入新笔记或新联系人。但是,当我插入新笔记时,我希望它出现在表格顶部,并将所有现有笔记向下移动一行。有什么方法可以将一行添加到一组特定的列中,以便保留在具有同一行但位于不同表中的单元格上的任何其他数据不会被移动?

回答by Helen Toomik

A basic example:

一个基本的例子:

Dim rng As Range
Set rng = Worksheets("Sheet1").Range("A2:D2")
rng.Insert Shift:=xlDown

...assuming that you want to insert 1 row of cells above the cells currently located in A2:D2.

...假设您要在当前位于 A2:D2 中的单元格上方插入 1 行单元格。

回答by thomaspaulb

You'll have to code a VBA macro that selects all cells in the notes table as a Range object, and then moves them down one row.

您必须编写一个 VBA 宏,该宏将笔记表中的所有单元格选择为 Range 对象,然后将它们向下移动一行。

I wouldn't know the exact code, but I'd advise you use the "Record Macro" function and do it by hand: select two cells in an Excel sheet, press the right mouse button and click "Insert". Then it'll ask you if you want to move the rows down. You can see in the newly recorded macro what the code is, and adjust it for your specific purpose. Good luck!

我不知道确切的代码,但我建议您使用“记录宏”功能并手动完成:在 Excel 工作表中选择两个单元格,按鼠标右键并单击“插入”。然后它会询问您是否要将行向下移动。您可以在新录制的宏中看到代码是什么,并根据您的特定目的进行调整。祝你好运!