|
-
Oct 31st, 2019, 12:41 AM
#1
Thread Starter
Hyperactive Member
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
-
Oct 31st, 2019, 01:58 AM
#2
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
-
Oct 31st, 2019, 03:58 AM
#3
Thread Starter
Hyperactive Member
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.
 Originally Posted by Zvoni
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
-
Oct 31st, 2019, 04:39 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|