在Excel VBA中插入CopyOrigin

时间:2022-04-13 04:57:06

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

有人能告诉我插入的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.

表达式。需要插入(转变,CopyOrigin)表达式。返回范围对象的表达式。

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.

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

CopyOrigin Optional Variant. The copy origin.

CopyOrigin可选的变体。复制来源。

3 个解决方案

#1


22  

It takes either of one parameter as given below.

它接受一个参数中的任何一个,如下所示。

Const xlFormatFromLeftOrAbove = 0

Member of Excel.XlInsertFormatOrigin

and...

和…

Const xlFormatFromRightOrBelow = 1

Member of Excel.XlInsertFormatOrigin

#2


20  

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".

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

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

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

#3


0  

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())

#1


22  

It takes either of one parameter as given below.

它接受一个参数中的任何一个,如下所示。

Const xlFormatFromLeftOrAbove = 0

Member of Excel.XlInsertFormatOrigin

and...

和…

Const xlFormatFromRightOrBelow = 1

Member of Excel.XlInsertFormatOrigin

#2


20  

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".

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

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

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

#3


0  

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())