views:

137

answers:

1

For eg: I have 50 rows of data. In that 1st row is having names of the students. I need the code to copy the data from RAM to RAMESH. In between I have 20 rows. I need the code to copy the rows and paste the same into another sheet. And It should not ask me for the names. By default, it has to take the names as RAM & RAMESH.

A: 

OK, the following assumes you have a sheet StartSheet and the one you want to copy to is called TargetSheet. It starts in Row 1, column 1, and goes through each row. If a name matches, it assigns that rownum to a variable. Then, using the two rownums, the rows are selected, copied, and pasted. This code does not care about the number of rows between the names, it simply checks for the names themselves. I am assuming the first column has the names, not, as you described, the first row.

Sub MoveRows()
   Dim rownum As Integer
   Dim colnum As Integer
   Dim startrow As Integer
   Dim endrow As Integer

    rownum = 1
    colnum = 1

     With ActiveWorkbook.Worksheets("StartSheet")
        Do
           If .Cells(rownum, 1).Value = "RAM" Then
              startrow = rownum
           End If
           rownum = rownum + 1
        Loop Until .Cells(rownum, 1).Value = "RAMESH"
        endrow = rownum
        ActiveWorkbook.Worksheets("StartSheet").Range(startrow & ":" & endrow).Copy

     End With
     ActiveWorkbook.Worksheets("TargetSheet").Paste




End Sub
moleboy