+ Reply to Thread
Results 1 to 9 of 9

How to take data from columns in some rows and insert in new row?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-30-2010
    Location
    South Pole
    MS-Off Ver
    Excel 2010
    Posts
    99

    Question How to take data from columns in some rows and insert in new row?

    I've got a big table that looks like this:

    A B C
    1 USA English Spanish
    2 England English
    3 Canada English French

    Now whenever there is any data present in column C I need to copy the row's content into a new row below, and insert the data in column B. So it looks like this:

    A B C
    1 USA English
    2 USA Spanish
    3 England English
    4 Canada English
    5 Canada French

    I have been messing around but can't find a good way to accomplish this. Would anyone please help me?

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

    Re: How to take data from columns in some rows and insert in new row?

    I have got a VBA code for you.
    Option Explicit
    
    Sub convert_data()
    Dim lrow As Long
    Dim lcol As Long
    Dim i As Long
    Dim a As Long
    Dim j As Long
    
    a = 1
    With Worksheets("Sheet1")
        lrow = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 1 To lrow
            lcol = .Range("IV" & i).End(xlToLeft).Column
            For j = 2 To lcol
                If lcol > 2 Then
                    Worksheets("Sheet2").Cells(a, 1).Value = .Cells(i, 1).Value
                    Worksheets("Sheet2").Cells(a, 2).Value = .Cells(i, j).Value
                    a = a + 1
                Else
                    Worksheets("Sheet2").Cells(a, 1).Value = .Cells(i, 1).Value
                    Worksheets("Sheet2").Cells(a, 2).Value = .Cells(i, j).Value
                    a = a + 1
                    Exit For
                End If
            Next j
        Next i
    End With
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button.
    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 tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: How to take data from columns in some rows and insert in new row?

    This seems to work:

    Insert this macro into a new module and run the macro CleanData
    Sub CleanData()
    Dim r As Range
    Dim i As Long
    Dim a As String, c As String
    
    Set r = Range("A1")
    
    'Starting at first row data loop through till column A cell is blank
    For i = 0 To 65536  'loop to some arbitrary ridicously high number
       If r.Offset(i, 0).Value = "" Then
          Exit For 'no more rows to process
       End If
       If r.Offset(i, 2).Value <> "" Then 'We have to copy this as a new row
          'fill variables with what we want to copy
          a = r.Offset(i, 0)
          c = r.Offset(i, 2)
          'insert a new row below current
          r.Offset(i + 1, 0).EntireRow.Insert
          'copy values to new row
          r.Offset(i + 1, 0).Value = a
          r.Offset(i + 1, 1).Value = c
          'clear c column value
          r.Offset(i, 2).Value = ""
       End If
    Next
    End Sub
    Ted
    "Live Long and Prosper"

  4. #4
    Registered User
    Join Date
    07-30-2010
    Location
    South Pole
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: How to take data from columns in some rows and insert in new row?

    Wow! Many thanks for your reply! Although I used to program in my teens I'm afraid I can't tweak your scripts so that they fit the actual worksheet I'm working with. The example I gave was simplified (as I thought the answer was simple as well).

    The actual table has data in columns A-Q. The columns with data I need to move to a new row (if applicable) are J, K & L. Every time there is something in either of these columns each cell needs a new row with column A copied to the new row. So if there is data in column J then 1 new row is needed, if there is data in J & K then 2 new rows are needed, etc.

    I tried modifying both your scripts but without success. Would give me a hint what variables to change for this?

    Again, a big thank you!

  5. #5
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: How to take data from columns in some rows and insert in new row?

    What about columns B-I and M-Q do they need to be copied down as well?

    Does the data in J-L need to be cleared?

    A small sample workbook with the raw data and another sheet of what you want it to look like would be of great help in assisting you.

  6. #6
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: How to take data from columns in some rows and insert in new row?

    Took a guess on what you wanted. The code is not very efficient but works. I attached a work book with the macro ...
    FunkyCopyDown.xlsm

    Sub CleanData()
    Dim r As Range
    Dim i As Integer, j As Long
    
    Set r = Range("A1")
    
    'Starting at first row data loop through till column A cell is blank
    For j = 0 To 65536  'loop to some arbitrary ridicously high number
       If r.Offset(j, 0).Value = "" Then
          Exit For 'no more rows to process
       End If
       i = 0
       If r.Offset(j, 9).Value <> "" Then 'something in J
          i = i + 1
          InsertNewRow Range(r.Offset(j, 0).Address), 10, r.Offset(j, 9).Value, i
       End If
       If r.Offset(j, 10).Value <> "" Then 'something in K
          i = i + 1
          InsertNewRow Range(r.Offset(j, 0).Address), 11, r.Offset(j, 10).Value, i
       End If
       If r.Offset(j, 11).Value <> "" Then 'Something in L
          i = i + 1
          InsertNewRow Range(r.Offset(j, 0).Address), 12, r.Offset(j, 11).Value, i
       End If
    
    Next
    End Sub
    
    Sub InsertNewRow(therange As Range, col As String, val As String, rT As Integer)
    'col will be the col number  ie J = col 10, K=11, L=12
    'val will be the associated val for the column
    'rT will be the number of inserted rows
    
    'Insert Row
    therange.Offset(rT, 0).EntireRow.Insert
    'Copy the value of cell A above it down
    therange.Offset(rT, 0).Value = therange.Offset(0, 0).Value
    'Copy the value j,k,or L to Cell I on the new row
    therange.Offset(rT, 8).Value = val
    'clear the value above where it came from
    therange.Offset(0, col - 1).Value = ""
    End Sub

  7. #7
    Registered User
    Join Date
    07-30-2010
    Location
    South Pole
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: How to take data from columns in some rows and insert in new row?

    This seems to solve my troubles, wow. Many thanks for your swift help! I wouldn't mind sending you money for a beer if you have a way to donate to you.

    Cheers, and thank you!

    I will test this on my live data tomorrow and report back.

  8. #8
    Registered User
    Join Date
    07-30-2010
    Location
    South Pole
    MS-Off Ver
    Excel 2010
    Posts
    99

    Re: How to take data from columns in some rows and insert in new row?

    Quote Originally Posted by janschepens View Post
    I will test this on my live data tomorrow and report back.
    This works just as I want it to, thank you again!

  9. #9
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: How to take data from columns in some rows and insert in new row?

    I am glad.... just bear in mind that I took a brute force approach to your problem. The code is done without any finesse or thought of efficiency.

    Enjoy!

+ 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