+ Reply to Thread
Results 1 to 8 of 8

Moving a col from one location to another using vba ......really slow.....can faster

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Moving a col from one location to another using vba ......really slow.....can faster

    i have a need to move one col of data to be the "first col" on the sheet. But its really slow........is there a faster way to do this..........here is the code.

    Note: I have some "msgbox" code in the code below so i can see how long things take.


    Please Login or Register  to view this content.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Moving a col from one location to another using vba ......really slow.....can fas

    I suspect that all those functions that you are calling.

    BTW, if alphacol changes 1 to "A", 2 to "B" etc.

    the expression
    Columns(alphacol(Study_Col_number) ":" & alphacol(Study_Col_number))
    could be replaced with
    Columns(Study_Col_number)

    Similarly Range(alphacol(Study_Col_number) & "1") could be replaced by
    Cells(1, Study_Col_number)

    The biggest slower downer that I see is that you a cutting and inserting a whole column, including a lot of empty cells.

    Perhaps
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Moving a col from one location to another using vba ......really slow.....can fas

    This is why i love this site! Thanks for the other information!!!!!!!!!!

    What would you recommend as far as cutting and copying.......i can determine a specific range of cells and cut-n-paste that if you think it would help.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Moving a col from one location to another using vba ......really slow.....can fas

    If you look at the code, the range being copied/inserted is restricted to the first last_row_in_study rows by the .Resize.

    That's what I recomend.

    Alternatly,you might delete all the columns to the left of the Study_Col_number, unless you need to save the data in those columns

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Moving a col from one location to another using vba ......really slow.....can fas

    I tried your code .......sorry for not getting back to you sooner......been busy today.

    Nice code. Much faster. I did have one question about the resize function........how does it work?

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Moving a col from one location to another using vba ......really slow.....can fas

    Help explains that aRange.Resize(rCount, cCount) is the rCount rows by cCount column range that has the same upper left cell as aRange.
    If either of the arguments is omitted, the resulting range has the same number of rows or columns as aRange.

  7. #7
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,662

    Re: Moving a col from one location to another using vba ......really slow.....can fas

    Thanks for the info. Still i have a few more questions regarding "resize". I hope you dont mind.

    Lets say you have a range
    temprange which is equal to A1:C5

    temprange.resize(1,3)

    this will increase your original range by 1 row and 3 cols.............

    is this right?

    so for this code

    Please Login or Register  to view this content.

    can you explain this......not sure i understand it really but let me try.

    So for the cut: Your focusing on the "study_col_number" and your resizing that col by increasing the rows by "last_row_in_study_col" and number of cols increasing by 1............see this does not make sense .....i must be thinking about this wrong.

    can you help?

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Moving a col from one location to another using vba ......really slow.....can fas

    Lets say you have a range , temprange, which is equal to A1:C5

    temprange.resize(1,3)
    temprange will be the 1 row X 3 column range, whose UL cell is A1, that is A1:C1.

    If Study_Col_number is 5 and last_row_in_study_col is 120
    Please Login or Register  to view this content.
    cuts the range myWorksheet!E1:E120
    Please Login or Register  to view this content.
    is redundant, it is same as
    Please Login or Register  to view this content.
    Which inserts that into A1:A120

+ 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