See comments in code below. Made some minor improvements besides the comments.
You could also change
from
.Cells(i, "A").Value
to
.Range("A" & i).value
I like to use Range because I'm only thinking the same way (colun then row).
I find it is a little confusing to keep on switch back from: "column then
row" - to: "row then column". This is a preference and is not a problem. I
tried to always use RANGE and not CELLS. sometimes you have to use cells()
method.
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")
'----------------- ADDED ------------------------------------
Set CriteriaSH = Sheets("Master Template")
TimeLine = CriteriaSH.Range("B5")
If TimeLine <> 60 And _
TimeLine <> 90 And _
TimeLine <> 120 Then
MsgBox ("Incorrect TimeLine")
Exit Sub
End If
'----------------- END ------------------------------------
For Each ce In Range("B15:B80")
If ce = "Yes" Then
'------------------ CHANGED FROM WORKSHEET FUNCTION -------------
Set c = TemplateSH.Rows("1:1").Find( _
what:=ce.Offset(0, -1).Value, _
LookIn:=xlValues, _
lookat:=xlWhole)
If c Is Nothing Then
MsgBox ("Could not find : " & ce.Offset(0, -1).Value)
Exit Sub
Else
DataCol = c.Column
End If
'------------------ END -------------
With TemplateSH
For i = 2 To 700
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, "A").Value = .Cells(i, "A").Value
OutSH.Cells(OutRow, "B").Value = .Cells(i, "D").Value
OutSH.Cells(OutRow, "C").Value = .Cells(i, "P").Value
OutSH.Cells(OutRow, "D").Value = .Cells(i, "E").Value
OutSH.Cells(OutRow, "I").Value = .Cells(i, "BQ").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)
'moved outrow to this location and added counter inside loop
OutRow = OutSH.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
With TemplateSH
For i = LBound(arr) To UBound(arr)
.Cells(arr(i), "A").Copy _
Destination:=OutSH.Cells(OutRow, "A")
'Duplicate of above row, eliminate
'OutSH.Cells(OutRow, "A").Value = .Cells(arr(i), "A").Value
.Cells(arr(i), "D").Copy _
Destination:=OutSH.Cells(OutRow, "B")
'Duplicate of above row, eliminate
'OutSH.Cells(OutRow, "B").Value = .Cells(arr(i), "D").Value
.Cells(arr(i), "J").Copy _
Destination:=OutSH.Cells(OutRow, "C")
'Duplicate of above row, eliminate
'OutSH.Cells(OutRow, "C").Value = .Cells(arr(i), "J").Value
.Cells(arr(i), "E").Copy _
Destination:=OutSH.Cells(OutRow, "D")
'Duplicate of above row, eliminate
'OutSH.Cells(OutRow, "D").Value = .Cells(arr(i), "E").Value
'--------------------------- New Code -----------------------
Select Case TimeLine
Case 60
.Cells(arr(i), "H").Copy _
Destination:=OutSH.Cells(OutRow, "E")
Case 90
.Cells(arr(i), "K").Copy _
Destination:=OutSH.Cells(OutRow, "E")
Case 120
.Cells(arr(i), "N").Copy _
Destination:=OutSH.Cells(OutRow, "E")
End Select
'---------------------------End -----------------------------
.Cells(arr(i), "BQ").Copy _
Destination:=OutSH.Cells(OutRow, "I")
'Duplicate of above row, eliminate
'OutSH.Cells(OutRow, "I").Value = .Cells(arr(i), "BQ").Value
'added row below
OutRow = OutRow + 1
Next i
End With
'sort output data
Application.StatusBar = "Sorting Output"
With OutSH
'-------------------------- CHANGED ------------------------------
'change this statement
.Range("A6:J" & (OutRow - 1)).Sort _
key1:=.Range("A6"), _
order1:=xlAscending, _
header:=xlYes
'---------------------------- ENd ---------------------------------
End With
Application.StatusBar = False
Sheets("Internal Project Plan").Select
Call Colors
Call Module6.SaveAs
End Sub