Copy column data matching the column name

Closed
spaceenigma Posts 1 Registration date Tuesday January 27, 2009 Status Member Last seen January 27, 2009 - Jan 27, 2009 at 10:32 PM
 Helper - Feb 4, 2009 at 07:42 PM
Hello,

In Excel 2003/XP/2000/97, how do I write a macro that will look for selected column headers . If found, it will copy that entire column to a new sheet in that same file?

For example, if my column headers look like this,

"Product ID" "Product Name" "Unit Price" "Units In Stock" "Description"

I need to pull all the rows under ProductID, UnitPrice, 'Units in stock" into a new sheet and group the identical products with Total of "units in stock"

Thanks,

1 response

I am assuming the sheet you want to transfer to has the three heading names in the order you stated.
Hope this helps.


Private Sub TransferRows()

Set s = Sheets("Sheet1")
Set t = Sheets("Sheet2")
Dim i
Dim b
i = 2
b = 1


Do Until IsEmpty(s.Range("A" & i))

If s.Range("A1") = t.Range("A1") And s.Range("C1") = t.Range("B1") And s.Range("D1") = t.Range("C1") Then
b = b + 1
t.Range("A" & b) = s.Range("A" & i)
t.Range("B" & b) = s.Range("C" & i)
t.Range("C" & b) = s.Range("D" & i)
End If

i = i + 1

Loop

t.Range("A1").Sort _
Key1:=t.Columns("A"), _
Header:=xlGuess


End Sub
2