Dear All,
i am trying to copy columns form sheet called "Productivity_Project_Record" to "Project List" only those columns whose heading are there in sheet "Headers" ie., from A1 to A55.
Here is my code i am struggling with...
can any one of you pls help me on this
Private Sub copyWithSelectedHeaders()
Dim row As Long
Dim column As Long
Dim copiedRow As Long
Dim fromSht As Worksheet
Dim toSht As Worksheet
Dim headerSht As Worksheet
fromSht = Sheets("Productivity_Project_Record")
toSht = Sheets("Project List")
headerSht = Sheets("Headers")
copiedRow = 2
Dim rgFoundCell As Range
'Assumption is that the rowheaders are available in the first row, which is skipped.
For row = 2 To fromSht.UsedRange.Rows.Count
For column = 1 To fromSht.UsedRange.Columns.Count
If fromSht.Cells(row, column) <> "" Then
rgFoundCell = Worksheets("Headers").Range("headings").Find(what:=fromSht.Cells(1, column))
If (rgFoundCell Is Nothing) Then
'column header not found
Else
rgFoundCell = toSht.Range("A1:BZ1").Find(what:=fromSht.Cells(1, column))
If rgFoundCell Is Nothing Then
'some thing wrongas thereisno matchingheader in tosheet.
Else
toSht(copiedRow, rgFoundCell.column).Cells = fromSht.Cells(row, column)
copiedRow = copiedRow + 1
End If
End If
End If
Next column
Next row
End Sub
Thanking you in advance,
Dee
i am trying to copy columns form sheet called "Productivity_Project_Record" to "Project List" only those columns whose heading are there in sheet "Headers" ie., from A1 to A55.
Here is my code i am struggling with...
can any one of you pls help me on this
Private Sub copyWithSelectedHeaders()
Dim row As Long
Dim column As Long
Dim copiedRow As Long
Dim fromSht As Worksheet
Dim toSht As Worksheet
Dim headerSht As Worksheet
fromSht = Sheets("Productivity_Project_Record")
toSht = Sheets("Project List")
headerSht = Sheets("Headers")
copiedRow = 2
Dim rgFoundCell As Range
'Assumption is that the rowheaders are available in the first row, which is skipped.
For row = 2 To fromSht.UsedRange.Rows.Count
For column = 1 To fromSht.UsedRange.Columns.Count
If fromSht.Cells(row, column) <> "" Then
rgFoundCell = Worksheets("Headers").Range("headings").Find(what:=fromSht.Cells(1, column))
If (rgFoundCell Is Nothing) Then
'column header not found
Else
rgFoundCell = toSht.Range("A1:BZ1").Find(what:=fromSht.Cells(1, column))
If rgFoundCell Is Nothing Then
'some thing wrongas thereisno matchingheader in tosheet.
Else
toSht(copiedRow, rgFoundCell.column).Cells = fromSht.Cells(row, column)
copiedRow = copiedRow + 1
End If
End If
End If
Next column
Next row
End Sub
Thanking you in advance,
Dee