INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Correct code ?

Correct code ?

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

RE: Correct code ?

I started with a sheet like:
   A    B    C    D    E    F    G
1  1    2    3    4    5    6
 
And then ran this code:

CODE --> vba

Sub InsertColumnLR()
    Dim LR As String
    LR = "C"
    ActiveSheet.Columns(LR).Insert Shift:=xlToLeft
End Sub 

I ended up with:

   A    B    C    D    E    F    G
1  1    2         3    4    5    6
 
A column was inserted at C as expected.

Duane
Hook'D on Access
MS Access MVP

RE: Correct code ?

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.

RE: Correct code ?

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

RE: Correct code ?

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close