Results 1 to 4 of 4

Thread: vba to move data from a row to a column

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    vba to move data from a row to a column

    I have a row of data with some cells that need to be moved into a column formation. For simplicity, lets say it is only 4 adjacent cells that need to be moved into a column listed under the position of the first cell . The column position of this data is always in the same location for every row. As an example, I would receive this .... 123456,123456,123456,123456 and the data would need to be moved into a column so that it looks like this ....
    123456
    123456
    123456
    123456

    I have considered using OffSet but without any success. A vba that would automatically add the necessary three blank rows below the initial row to allow the insertion of the transposed data would also be great to have. The file size for this sheet is just over 65k lines and that is a lot of right-clicking to insert lines and transpose the cells. I have struggled with this for a couple of days and have run out of others seeking help. Your assistance is greatly appreciated and thanked.

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Robert, welcome to ExcelFox community.

    Can you post a sample file on a file sharing site, and post the link here. It would be better to have a look at the file, and propose a solution.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Sample file attached, thanks.

    Thanks for the willingness to assist a newbie!
    Quote Originally Posted by Excel Fox View Post
    Robert, welcome to ExcelFox community.

    Can you post a sample file on a file sharing site, and post the link here. It would be better to have a look at the file, and propose a solution.
    Attached Files Attached Files

  4. #4
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    14
    Here you go:

    Change constant variable values accordingly (Highlighted with red color)

    Code:
    Sub Lalit_Test()
    
        Dim varData()               As Variant
        Dim varFinalData()          As Variant
        Dim lngTotalDataCell        As Long
        Dim lngLoop                 As Long
        Dim lngLoop1                As Long
        Dim lngCount                As Long
        
        Const strDataRange          As String = "$A$6:$E$9"
        Const strDataShtName        As String = "Sheet1"
        Const strOutDataCell        As String = "$K$13"
        
        With ThisWorkbook.Worksheets(strDataShtName)
            .Range(strOutDataCell).Resize(.Rows.Count - .Range(strOutDataCell).Row + 1, 2).ClearContents
            varData = .Range(strDataRange).Value
            lngTotalDataCell = WorksheetFunction.CountA(.Range(strDataRange)) - .Range(strDataRange).Rows.Count
            ReDim varFinalData(1 To lngTotalDataCell, 1 To 2)
            lngCount = 0
            For lngLoop = LBound(varData) To UBound(varData)
                varFinalData(lngCount + 1, 1) = varData(lngLoop, LBound(varData))
                For lngLoop1 = LBound(varData) + 1 To UBound(varData, 2)
                    If LenB(Trim(varData(lngLoop, lngLoop1))) Then
                        lngCount = lngCount + 1
                        varFinalData(lngCount, 2) = varData(lngLoop, lngLoop1)
                    End If
                Next lngLoop1
            Next lngLoop
            If lngCount Then
                .Range(strOutDataCell).Resize(UBound(varFinalData), UBound(varFinalData, 2)).Value = varFinalData
            End If
        End With
        
        Erase varData
        Erase varFinalData
        lngTotalDataCell = Empty
        lngLoop = Empty
        lngLoop1 = Empty
        lngCount = Empty
    
    End Sub
    Last edited by LalitPandey87; 03-03-2014 at 09:23 AM.

Similar Threads

  1. Replies: 0
    Last Post: 12-24-2013, 01:36 PM
  2. Replies: 14
    Last Post: 08-08-2013, 04:53 PM
  3. VBA code to move row to new spreadsheet
    By cdurfey in forum Excel Help
    Replies: 6
    Last Post: 06-10-2013, 10:38 PM
  4. Replies: 7
    Last Post: 05-17-2013, 10:38 PM
  5. Replies: 1
    Last Post: 08-07-2012, 11:04 PM

Tags for this Thread

Posting Permissions

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