Results 1 to 4 of 4

Thread: Singe Row to Multiple Row

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Single Row to Multiple Row

    I've read some of the other posts on this question, but I can't seem to figure out how to make this work for my scenario.
    I have a spreadsheet with multiple rows that looks like this.

    Title1 Desc1 Value1 Value1 Value2 Value2 Value3 Value3
    Title2 Desc2 Value1 Value1 Value2 Value2 Value3 Value3
    Title3 Desc3 Value1 Value1 Value2 Value2 Value3 Value3

    I need it to look like this . . .

    Title1 Desc1 Value1 Value1
    Title1 Desc1 Value2 Value2
    Title1 Desc1 Value3 Value3
    Title2 Desc2 Value1 Value1
    Title2 Desc2 Value2 Value2
    Title2 Desc2 Value3 Value3
    Title3 Desc3 Value1 Value1
    Title3 Desc3 Value2 Value2
    Title3 Desc3 Value3 Value3


    There are more than 3 "values" on each line, although not a fixed number. It could be 10, it could be 200. I basically need a new line for each pair of values for each Title/Desc.

    I may be making the question too complicate, but I think the visual of the spreadsheet before and after helps.

    Any assistance would be appreciated.
    Last edited by jerry2334; 04-17-2013 at 10:04 PM. Reason: word in title misspelled

  2. #2
    Senior Member LalitPandey87's Avatar
    Join Date
    Sep 2011
    Posts
    222
    Rep Power
    14
    Try this:
    Code:
    Sub LMP_Test()
    
        Dim rngRange                As Range
        Dim varArrayData()          As Variant
        Dim varArrayFinal()         As Variant
        Dim lngLoop1                As Long
        Dim lngLoop2                As Long
        Dim lngCount                As Long
        Dim strValue1               As String
        Dim strValue2               As String
        
        Const strShtName            As String = "Sheet1" 'Change Accordingly
        Const strDataStartCell      As String = "A1" 'Change Accordingly
        Const strFinalDataCell      As String = "P1" 'Change Accordingly
        
        With Worksheets(strShtName)
            Set rngRange = .Range(strDataStartCell).CurrentRegion
            If rngRange.Rows.Count > 1 And rngRange.Columns.Count > 2 Then
                varArrayData = rngRange.Value
                ReDim varArrayFinal(1 To (UBound(varArrayData) * (UBound(varArrayData, 2) - 2)), 1 To 4)
                lngCount = 1
                For lngLoop1 = LBound(varArrayData) To UBound(varArrayData)
                    For lngLoop2 = LBound(varArrayData) + 2 To UBound(varArrayData, 2)
                        varArrayFinal(lngCount, 1) = varArrayData(lngLoop1, 1)
                        varArrayFinal(lngCount, 2) = varArrayData(lngLoop1, 2)
                        If lngLoop2 <= UBound(varArrayData, 2) Then
                            If varArrayData(lngLoop1, lngLoop2) <> "" Then
                                varArrayFinal(lngCount, 3) = varArrayData(lngLoop1, lngLoop2)
                            End If
                        End If
                        lngLoop2 = lngLoop2 + 1
                        If lngLoop2 <= UBound(varArrayData, 2) Then
                            If varArrayData(lngLoop1, lngLoop2) <> "" Then
                                varArrayFinal(lngCount, 4) = varArrayData(lngLoop1, lngLoop2)
                            End If
                        End If
                        lngCount = lngCount + 1
                    Next lngLoop2
                Next lngLoop1
                Set rngRange = .Range(strFinalDataCell).Resize(, UBound(varArrayFinal, 2))
                rngRange.EntireColumn.ClearContents
                rngRange.Resize(UBound(varArrayFinal), UBound(varArrayFinal, 2)).Value = varArrayFinal
            End If
        End With
        
        Set rngRange = Nothing
        Erase varArrayData
        Erase varArrayFinal
        lngLoop1 = Empty
        lngLoop2 = Empty
        lngCount = Empty
        strValue1 = vbNullString
        strValue2 = vbNullString
    
    End Sub

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Thanks for such a quick response. I updated the constants and ran the macro but nothing happened. No errors.

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Can you post the sample workbook?
    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

Similar Threads

  1. Replies: 3
    Last Post: 05-23-2013, 11:17 PM
  2. Autofilling in VBA through the last row
    By jardenp in forum Excel Help
    Replies: 7
    Last Post: 03-29-2013, 09:46 PM
  3. Replies: 4
    Last Post: 03-22-2013, 01:47 PM
  4. Replies: 2
    Last Post: 03-05-2013, 07:34 AM
  5. Column to Row and Row to Column
    By lokvan in forum Excel Help
    Replies: 1
    Last Post: 11-30-2012, 09:30 AM

Posting Permissions

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