在 Excel VBA 中插入时复制原点

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

CopyOrigin on Insert in Excel VBA

excelvbaexcel-vba

提问by Craig T

Can anyone tell me what the CopyOrigin parameter of Insert is used for? And what values it will accept?

谁能告诉我 Insert 的 CopyOrigin 参数是做什么用的?它会接受什么样的价值观?

I have included the vba help (which wasn't really that helpful):

我已经包含了 vba 帮助(这并不是很有帮助):

Inserts a cell or a range of cells into the worksheet or macro sheet and shifts other cells away to make space.

expression.Insert(Shift, CopyOrigin) expression Required. An expression that returns a Range object.

Shift Optional Variant. Specifies which way to shift the cells. Can be one of the following XlInsertShiftDirection constants: xlShiftToRight or xlShiftDown. If this argument is omitted, Microsoft Excel decides based on the shape of the range.

CopyOrigin Optional Variant. The copy origin.

将一个单元格或一系列单元格插入到工作表或宏表中,并将其他单元格移开以腾出空间。

expression.Insert(Shift, CopyOrigin) 表达式 必需。返回 Range 对象的表达式。

Shift 可选变体。指定移动单元格的方式。可以是以下 XlInsertShiftDirection 常量之一:xlShiftToRight 或 xlShiftDown。如果省略此参数,Microsoft Excel 将根据范围的形状进行决定。

CopyOrigin 可选变体。副本原点。

回答by lakshmanaraj

It takes either of one parameter as given below.

它采用以下任一参数。

Const xlFormatFromLeftOrAbove = 0

Member of Excel.XlInsertFormatOrigin

and...

和...

Const xlFormatFromRightOrBelow = 1

Member of Excel.XlInsertFormatOrigin

回答by shahkalpesh

Adding to Lakshmanaraj's comments - it picks up the formatting option depending on where you are inserting cells & what formatting you wish to pick.

添加到 Lakshmanaraj 的评论中 - 它根据您插入单元格的位置以及您希望选择的格式来选择格式选项。

Lets say you have:
first row which has bold text,
second row has things in italic.
You select the 2nd row & execute the following expression:

假设您有:
第一行有粗体文字,
第二行有斜体字。
您选择第二行并执行以下表达式:

Selection.Insert CopyOrigin:=xlFormatFromLeftOrAbove

The new row gets inserted between 1st and 2nd row & it picks formatting rules from the "row above" or "cells to the left of the cell".

新行插入第一行和第二行之间,并从“上面的行”或“单元格左侧的单元格”中选择格式规则。

In this case, the newly inserted cells will have text as bold without you setting it explicitly.

在这种情况下,新插入的单元格将具有粗体文本,而无需您明确设置。

回答by user712793

You can Reference here :

你可以参考这里:

Imports Excel = Microsoft.Office.Interop.Excel
Dim XLApp As New Excel.Application()
Dim xWkBook As Excel.Workbook = XLApp.Workbooks.Open(YourInitialPath)
Dim xSheet As Excel.Worksheet = CType(xWkBook.Sheets(1), Excel.Worksheet)

CurCell = xSheet.Range("G9:G11")
CurCell.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, CurCell.Copy())