PDA

View Full Version : Two Columns (Search and Copy)



demitris
07-24-2011, 07:56 AM
I have a column (A1:A3257) With names and Another column with Names (C1:C5288). The names in the first column are included in the second column. Next to the Names of the Second Column, I have a third column (D1:5288) with Values (eg. Grades). I want a code which looks for the name of the first column from the second column and copy the value of the third column (eg. Grades) to another column (doesnt matter the numer) in the excel sheet. In other words, I have 10 names and a column with 50 names with their grades. I want to search the names of the first column and find their grade from the column with 50 names.

I am really fresher in macro commands. Can anyone help me please? Thank you very much for your time.

Dem

Kenneth Hobs
07-24-2011, 10:41 AM
Welcome to the forum!

Why do you need vba when vlookup does the job? With column names in row 1, in B2: =VLOOKUP(A2,C1:D5288,2,)

CatDaddy
07-25-2011, 04:46 PM
Sub Grades()
ActiveWorkbook.Sheets(1).Activate
Range("A1").Activate
Dim cell As Range, cell2 As Range
Dim name As String
Dim crow As Long, i As Long, grade As Long, gT As Long
For Each cell In Range("A1:A5")
i = 2
name = cell.Value
crow = cell.row

For Each cell2 In Sheets(2).Range("A1:A25")

If cell2.Value = name Then
grade = cell2.Offset(0, 1).Value
Sheets(1).Cells(crow, i).Value = grade
gT = gT + grade
i = i + 1
End If
Next cell2
Sheets(1).Cells(crow, i).Value = gT
gT = 0
Next cell

End Sub


like this?