Results 1 to 3 of 3

Rearrange columns - loop perhaps?

Threaded View

tone640 Rearrange columns - loop... 10-20-2011, 08:58 AM
romperstomper Re: Rearrange columns - loop... 10-20-2011, 09:04 AM
tone640 Re: Rearrange columns - loop... 10-20-2011, 09:18 AM
  1. #1
    Forum Contributor
    Join Date
    06-15-2011
    Location
    Chester, England
    MS-Off Ver
    Excel 2013
    Posts
    117

    Rearrange columns - loop perhaps?

    The following macro takes information from one sheet and puts it on another (basically swaps the order of the columns):

    Sub SSETransfer()
    
        Dim rFirstBlank As Range
        Dim iRowCount As Integer
        Dim sCol As String
    
        Set rFirstBlank = Sheet1.Cells.SpecialCells(xlCellTypeLastCell)
        iRowCount = Sheet2.Range("A65536").End(xlUp).Row - 1
        
        sCol = "A"
        Sheet1.Range(sCol & rFirstBlank.Row + 1, sCol & rFirstBlank.Row + iRowCount) _
            = Sheet2.Range("A2", Range("A65536").End(xlUp).Offset(1, 0)).Value
        
        sCol = "B"
        Sheet1.Range(sCol & rFirstBlank.Row + 1, sCol & rFirstBlank.Row + iRowCount) _
            = Sheet2.Range("D2", Range("D65536").End(xlUp).Offset(1, 0)).Value
        
        sCol = "C"
        Sheet1.Range(sCol & rFirstBlank.Row + 1, sCol & rFirstBlank.Row + iRowCount) _
            = Sheet2.Range("R2", Range("R65536").End(xlUp).Offset(1, 0)).Value
        
        sCol = "D"
        Sheet1.Range(sCol & rFirstBlank.Row + 1, sCol & rFirstBlank.Row + iRowCount) _
            = Sheet2.Range("N2", Range("N65536").End(xlUp).Offset(1, 0)).Value
    
    End Sub
    It works perfectly, but only if Sheet2 is active - why is this?

    Secondly, I am doing the same operation for around 20 columns - could I turn this into a loop?

    Thanks for reading.
    Last edited by tone640; 10-20-2011 at 09:21 AM.

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