Help with some code

davio565

New Member
Joined
Jan 19, 2017
Messages
23
Hi Guys,

I'm trying to rearrange some data and having trouble with getting it to correctly loop through a list.

Basically I have data in columns 1-39 with no gaps. Column N contains names. I am trying to create something which takes the name on any given row (From N2 down) and searches for matches in all rows below given row. Each time it finds a match it copies all the data from each row and pastes it all on the same row which the original name came from. Then this would loop only the next cell so N3, take the name, find matched below paste all the the right etc.

So i can get the code to do the procedure i want one time, on N2, but then cant get that to loop through all the names AND find all the below matched and paste them on the same row.

At the moment it's looping through the N column (somehow) and pasting everything on the first row. I cant correctly make it offset the paste to BB3 etc. without getting an error.

This is what i have:

Code:
[/COLOR]Sub finddata()

Dim name As String
Dim finalrow As Integer
Dim startrow As Integer
Dim i As Integer
Dim offset As Integer


Sheets("Sheet1").Range("BB2:CAA5000").ClearContents


Range("BB2").Select
    ActiveCell.FormulaR1C1 = "0"
    Range("BC2").Select
    ActiveCell.FormulaR1C1 = "0"
    
name = Sheets("Sheet1").Range("N2").Value                          'Needs to loop down the N column from N2


startrow = Sheets("sheet1").Range("N2").Row                         'Needs to be the current row in the loop


finalrow = Sheets("sheet1").Range("C100000").End(xlUp).Row


Dim cell As Range


For Each cell In ActiveSheet.Range("N2:N1000")
   
For i = startrow To finalrow
If Cells(i, 14) = name Then
    Range(Cells(i, 1), Cells(i, 39)).Copy
    Range("BB2").Select
    Selection.End(xlToRight).Select
    Selection.offset(0, 1).PasteSpecial xlPasteValues
    End If
Next i


  Next cell
  
End Sub



It's a bit messy as the loop is sort of just stuck in there. Any help would be much appreciated.

Thanks
Dave
 
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
Sub finddata()

Dim name As String
Dim finalrow As Integer
Dim startrow As Integer
Dim i As Integer


Sheets("Sheet1").Range("BB2:CAA5000").ClearContents


Range("BB2").Select
    ActiveCell.FormulaR1C1 = "0"
    Range("BC2").Select
    ActiveCell.FormulaR1C1 = "0"
    
finalrow = Sheets("sheet1").Range("C100000").End(xlUp).Row


Dim cell As Range


For Each cell In ActiveSheet.Range("N2:N" & finalrow)
   
For i = cell.Row + 1 To finalrow
If Cells(i, 14) = cell.Value Then
    Range(Cells(i, 1), Cells(i, 39)).Copy
    Cells(cell.Row, Columns.Count).End(xlToLeft).Offset(,1).PasteSpecial xlPasteValues
    End If
Next i


  Next cell
  
End Sub
 
Last edited:
Upvote 0
Hi Fluff,

Very late on but just wanted to say thanks for this code you did ages ago. Just realised I never thanked you.

Worked really well and I managed to modify it a few times for my needs.

Thanks
Dave
 
Upvote 0
Glad to hear you've managed to modify to suit & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top