Hi All,
i found this code and it does 3/4 things i wanted
i.e.
* to copy/paste special values to rows based on column headers from 1 sheet to another
* works regardless of the column order
* pastes the data to the next empty row in the "Target Sheet" (for new data input)
However the requirement for this code is that the # of Column headers in "Target Sheet" must equal to & Match with the Column headers in "Source Sheet".
My dilemma is that the # of column headers in my "Target Sheet's" will vary but the ones it has will match with the ones in "Source Sheet". How do i adjust this code so that it does not require exact number of column headers on both sheets for it to run.
thanks guys,
i found this code and it does 3/4 things i wanted
i.e.
* to copy/paste special values to rows based on column headers from 1 sheet to another
* works regardless of the column order
* pastes the data to the next empty row in the "Target Sheet" (for new data input)
However the requirement for this code is that the # of Column headers in "Target Sheet" must equal to & Match with the Column headers in "Source Sheet".
My dilemma is that the # of column headers in my "Target Sheet's" will vary but the ones it has will match with the ones in "Source Sheet". How do i adjust this code so that it does not require exact number of column headers on both sheets for it to run.
thanks guys,
Code:
Sub CopyDataBlocks()
'VARIABLE NAME 'DEFINITION
Dim SourceSheet As Worksheet 'The data to be copied is here
Dim TargetSheet As Worksheet 'The data will be copied here
Dim ColHeaders As Range 'Column headers on Target sheet
Dim MyDataHeaders As Range 'Column headers on Source sheet
Dim DataBlock As Range 'A single column of data
Dim c As Range 'a single cell
Dim Rng As Range 'The data will be copied here (="Place holder" for the first data cell)
Dim i As Integer
'Change the names to match your sheetnames:
Set SourceSheet = Sheets("ws1")
Set TargetSheet = Sheets("ws2")
With TargetSheet
Set ColHeaders = .Range("A5:E5") '.Cells(1, 1), .Cells(1, .Columns.Count).End(xlToLeft)) 'Or just .Range("A1:C1")
Set Rng = .Cells(.Rows.Count, 1).End(xlUp).Offset(1) 'Shoots up from the bottom of the sheet untill it bumps into something and steps one down
End With
With SourceSheet
Set MyDataHeaders = .Range("A1:E1")
'Makes sure all the column names are the same:
'Each header in Source sheet must have a match on Target sheet (but not necessarily in the same order + there can be more columns in Target sheet)
For Each c In MyDataHeaders
If Application.WorksheetFunction.CountIf(ColHeaders, c.Value) = 0 Then
MsgBox "Can't find a matching header name for " & c.Value & vbNewLine & "Make sure the column names are the same and try again."
Exit Sub 'The code exits here if thereäs no match for the column header
End If
Next c
'There was a match for each colum name.
'Set the first datablock to be copied:
Set DataBlock = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp)) 'A2:A & the last cell with something on it on column A
'Resizes the target Rng to match the size of the datablock:
Set Rng = Rng.Resize(DataBlock.Rows.Count, 1)
'Copies the data one column at a time:
For Each c In MyDataHeaders
i = Application.WorksheetFunction.Match(c.Value, ColHeaders, 0) 'Finds the matching column name
Rng.Offset(, i - 1).Value = Intersect(DataBlock.EntireRow, c.EntireColumn).Value 'Writes the values
Next c
'Uncomment the following line if you want the macro to delete the copied values:
' Intersect(MyDataHeaders.EntireColumn, DataBlock.EntireRow).ClearContents
End With
End Sub