Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct code ?

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
I want to insert a column at the right of the column with variable LR but the code below inserts a column to the left of LR...?
Why doesn't this code work like I thought ?

Code:
ActiveSheet.Columns(LR).Insert Shift:=xlToLeft
 
I started with a sheet like:
[pre]
A B C D E F G
1 1 2 3 4 5 6
[/pre]
And then ran this code:
Code:
Sub InsertColumnLR()
    Dim LR As String
    LR = "C"
    ActiveSheet.Columns(LR).Insert Shift:=xlToLeft
End Sub

I ended up with:
[pre]

A B C D E F G

1 1 2 3 4 5 6
[/pre]
A column was inserted at C as expected.

Duane
Hook'D on Access
MS Access MVP
 
It looks like Shift:=xlToLeft and Shift:=xlToRight doesn't make a difference to the code. If LR is a number, just modify your code to

ActiveSheet.Columns(LR+1).Insert

if LR is a string it gets a lot more complicated.
 
The 'Insert' parameter applies to inserting range, existing cells can be shifted down or right, but always the insertion line will be on the top or left of reference range. 'Shift' can take one of XlInsertShiftDirection enum values: xlShiftToRight or xlShiftDown, there is no xlToLeft.
Apply Insert to next column instead (LR+1 if LR is the column's number).

combo
 
I solved the problem using the LR+1 option but I was wondering why the xlToLeft didn't work while xlToRight did work.
Thanks for this info.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top