Results 1 to 4 of 4

Thread: Find values in columns and cut and paste the range of cells from columns to rows

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Find values in columns and cut and paste the range of cells from columns to rows

    I have set of data which I want to transpose from columns to rows.. the data has same 17 headers and 25 rows of data for different companies one after another in columns and I want the same to be transposed into rows..so first I am finding the starting column name with the identifiable text and then from that column I am selecting 16 columns to right and 25 rows down range to cut and paste data .i.e. to append in the last row of Column D. I want to run this in loop till the all the columns data in right is cut and pasted in the rows one below another...but I run the below code which I have so far, it gets stuck in loop..


    Code:
    Sub test()
    Dim rng As Range
    Dim cell As Range
    Dim search As String
    Dim lRow As Long
    lRow = Cells(Rows.Count, 4).End(xlUp).Row + 1
    Set rng = ActiveSheet.Columns("U:XFD")
    search = "IS_AUDITOR"
    Set cell = rng.Find(What:=search, LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False)
    
    If cell Is Nothing Then
        Exit Sub
    Else
    Do While cell.Value <> ""
    cell.Select
     Range(ActiveCell, ActiveCell.Offset(25, 16)).Cut
     Range("D" & lRow).Select
     ActiveSheet.Paste
    
    End If
    Loop
    End Sub

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Find values in columns and cut and paste the range of cells from columns to rows

    You're not "moving" the cell/ActiveCell forward.
    Put "Set Cell = rng.Find....." incl. your "If Cell is nothing" into the loop (at the start of the loop)
    And put the Loop-condition at the end of the loop (Do .. Loop Until)

    EDIT: Untested!
    Code:
    Sub test()
    Dim rng As Range
    Dim cell As Range
    Dim search As String
    Dim lRow As Long
    lRow = Cells(Rows.Count, 4).End(xlUp).Row + 1
    Set rng = ActiveSheet.Columns("U:XFD")
    search = "IS_AUDITOR"
    Do
     Set cell = rng.Find(What:=search, LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False)
     If cell Is Nothing Then Exit Do
     cell.Select
     Range(ActiveCell, ActiveCell.Offset(25, 16)).Cut
     Range("D" & lRow).Select
     ActiveSheet.Paste
    Loop Until cell.Value = ""
    End Sub
    Last edited by Zvoni; Oct 31st, 2019 at 02:02 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2009
    Posts
    295

    Re: Find values in columns and cut and paste the range of cells from columns to rows

    It cuts the data correctly but keeps pasting the data in the same cell in column D which means it's overwriting previously pasted data, it's suppose to select the last blank cell in Column D everytime but it's not doing the same.

    Quote Originally Posted by Zvoni View Post
    You're not "moving" the cell/ActiveCell forward.
    Put "Set Cell = rng.Find....." incl. your "If Cell is nothing" into the loop (at the start of the loop)
    And put the Loop-condition at the end of the loop (Do .. Loop Until)

    EDIT: Untested!
    Code:
    Sub test()
    Dim rng As Range
    Dim cell As Range
    Dim search As String
    Dim lRow As Long
    lRow = Cells(Rows.Count, 4).End(xlUp).Row + 1
    Set rng = ActiveSheet.Columns("U:XFD")
    search = "IS_AUDITOR"
    Do
     Set cell = rng.Find(What:=search, LookIn:=xlFormulas, LookAt:=xlWhole, MatchCase:=False)
     If cell Is Nothing Then Exit Do
     cell.Select
     Range(ActiveCell, ActiveCell.Offset(25, 16)).Cut
     Range("D" & lRow).Select
     ActiveSheet.Paste
    Loop Until cell.Value = ""
    End Sub

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: Find values in columns and cut and paste the range of cells from columns to rows

    Move the lRow=Cells..... into the Loop as the first line after the "Do"
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

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
  •  



Click Here to Expand Forum to Full Width