+ Reply to Thread
Results 1 to 6 of 6

Change the column order

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2010
    Location
    ipswich,england
    MS-Off Ver
    Excel 2003
    Posts
    6

    Change the column order

    I am trying to change the order of columns in a spreadsheet ,I've stumbled across a piece of code that looks like it will do what i want but i get an error (application-defined or object-defined error) everytime i run it, any help would be appreciated.

    
    Sub Movecolumns() 
        Dim ori As Variant 
        Dim dst As Variant 
        Dim i As Integer 
        ori = Array("A", "B", "C") 
        dst = Array("C", "B", "A") 
        For i = LBound(ori) To UBound(ori) 
            Columns(ori(i)).Cut 
            Columns(dst(i)).Insert Shift:=xlToRight 
        Next 
        Range("A6").Select 
         
    End Sub

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Change the column order

    On which line do you get the error?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Change the column order

    Try this code
    Sub change_cols()
    
    With Worksheets(1)
        .Columns("A:A").Cut
        .Columns("D:D").Select
        .Paste
        .Columns("C:C").Cut
        .Columns("A:A").Select
        .Paste
        .Columns("C:C").Delete
    End With
    End Sub

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,320

    Re: Change the column order

    You can't cut column B to column B.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    12-09-2010
    Location
    ipswich,england
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Change the column order

    Thanks everyone for your quick replies, yes i realise now why it errors out and how to get around it.
    thanks again, its much appriciated.


    mike..

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Change the column order

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1