22

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

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

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.

3 Answers 3

26

It takes either of one parameter as given below.

Const xlFormatFromLeftOrAbove = 0

Member of Excel.XlInsertFormatOrigin

and...

Const xlFormatFromRightOrBelow = 1

Member of Excel.XlInsertFormatOrigin
Sign up to request clarification or add additional context in comments.

Comments

23

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

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.

Comments

-2

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

3 Comments

This answer is absolutely misleading, there's no reason to put CurCell.Copy() inside Insert call as it doesn't even return anything but copies CurCell contents to clipboard later to be inserted by CurCell.Insert()
curcell is what ? range variable ? planet variable ? Name of your wife ?
this is not vba, you can't set a variable while using DIM. everything is 'syntax error' color flagged in VBE (exept the line with range).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.