Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function CopyColumnsByHeaders(SourceWorkbook As Workbook, SourceWorksheet As Worksheet, _
- destWorkbook As Workbook, destWorksheet As Worksheet, _
- startColumn As Long, ParamArray headers() As Variant)
- Dim Header As Variant
- Dim sourceHeaderRange As Range
- Dim destHeaderRange As Range
- Dim lastUsedRow As Long
- Dim lastUsedColumn As Long
- Dim sourceLastRow As Long
- Dim destLastRow As Long
- For Each Header In headers
- Set sourceHeaderRange = SourceWorksheet.Rows(1).Find(Header, LookIn:=xlValues, LookAt:=xlWhole)
- If Not sourceHeaderRange Is Nothing Then
- sourceLastRow = SourceWorksheet.Cells(SourceWorksheet.Rows.Count, sourceHeaderRange.Column).End(xlUp).Row
- destLastRow = destWorksheet.Cells(destWorksheet.Rows.Count, startColumn).End(xlUp).Row + 1
- SourceWorksheet.Range(SourceWorksheet.Cells(2, sourceHeaderRange.Column), SourceWorksheet.Cells(sourceLastRow, sourceHeaderRange.Column)).SpecialCells(xlCellTypeVisible).Copy _
- Destination:=destWorksheet.Cells(destLastRow, startColumn)
- startColumn = startColumn + 1
- End If
- Next Header
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement