Private Sub CommandButton1_Click()
Dim OutSH As Worksheet, TemplateSH As Worksheet, ce As Variant
Dim DataCol As Integer, OutRow As Long, i As Long
Dim arr As Variant
Set OutSH = Sheets("Internal Project Plan")
Set TemplateSH = Sheets("Master Template")
For Each ce In Range("B13:B70")
If ce = "Yes" Then
DataCol = WorksheetFunction.Match(ce.Offset(0, -1).Value, TemplateSH.Rows("1:1"), 0)
With TemplateSH
For i = 2 To 650
If .Cells(i, DataCol).Value = "x" Then
'check to see if it already exists and only proceed if it does not
If WorksheetFunction.CountIf(OutSH.Range("A:A"), TemplateSH.Cells(i, 1).Value) = 0 Then
OutRow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
OutSH.Cells(OutRow, 1).Value = .Cells(i, 1).Value
OutSH.Cells(OutRow, 2).Value = .Cells(i, 4).Value
OutSH.Cells(OutRow, 3).Value = .Cells(i, 10).Value
OutSH.Cells(OutRow, 4).Value = .Cells(i, 5).Value
OutSH.Cells(OutRow, 10).Value = .Cells(i, 63).Value
End If
End If
Next i
End With
End If
Next ce
Application.StatusBar = "Transferring Headings"
arr = Array(2, 15, 77, 87, 461, 507, 534, 553, 582)
With TemplateSH
For i = LBound(arr) To UBound(arr)
OutRow = OutSH.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
.Cells(arr(i), 1).Copy Destination:=OutSH.Cells(OutRow, 1)
OutSH.Cells(OutRow, 1).Value = .Cells(arr(i), 1).Value
.Cells(arr(i), 4).Copy Destination:=OutSH.Cells(OutRow, 2)
OutSH.Cells(OutRow, 2).Value = .Cells(arr(i), 4).Value
.Cells(arr(i), 10).Copy Destination:=OutSH.Cells(OutRow, 3)
OutSH.Cells(OutRow, 3).Value = .Cells(arr(i), 10).Value
.Cells(arr(i), 5).Copy Destination:=OutSH.Cells(OutRow, 4)
OutSH.Cells(OutRow, 4).Value = .Cells(arr(i), 5).Value
.Cells(arr(i), 63).Copy Destination:=OutSH.Cells(OutRow, 10)
OutSH.Cells(OutRow, 10).Value = .Cells(arr(i), 63).Value
Next i
End With
'sort output data
Application.StatusBar = "Sorting Output"
With OutSH
.Range("A6:J" & .Cells(Rows.Count, 1).End(xlUp).Row).Sort key1:=.Range("A6"), order1:=xlAscending, header:=xlYes
End With
Application.StatusBar = False
End Sub
In Master Template!I461, you have an x in your heading row. Running this code on your latest example file will bring this heading over 2 times. I'm guessing that this X shouldn't be in that cell.
Bookmarks