Since I'm still in the process of building the code, I've been stepping through it with F8 and that's how I found the problem.
I apologize to your braincells but here's the entire code (you'll have to forgive the mess as I'm basically self taught on macros and probably haven't learned the most efficient methods for writing code)
Public SHT As Worksheet
Public PivotVS() As Variant, PivotVSQTY() As Integer
Public ProjectName As String, PivotVSTop() As Integer, PivotVSBottom() As Integer
-----------------------------------------------------
Sub PivotDataRevisit()
Dim PROJECT As Integer, VSGROUP As Integer
Dim PR() As Variant, VS() As Variant, VSTop() As Variant, VSBottom() As Variant
Dim PivotSheet As Worksheet
'<<<<<<<<Establish project names.>>>>>>>>
For Each Sheet In ActiveWorkbook.Sheets
'If Sheet.Visible <> xlSheetHidden Then Sheet.Select
If InStr(Sheet.Name, "PIVOT") > 0 Then
PROJECT = PROJECT + 1
ReDim Preserve PR(PROJECT)
PR(PROJECT) = Right(Sheet.Name, 3)
End If
Next Sheet
For PROJECT = 1 To 8
'<<<<<<<<Determine what Value Stream Groups are on the PivotTable.>>>>>>>>
Sheets("PIVOT " & PR(PROJECT)).Select
Set PivotSheet = ActiveSheet
NumberOfValueStreams
Sheet1.Select
'MsgBox "OK"
'<<<<<<<<Establish top and bottom of project on Layout Sheet.>>>>>>>>
If ProjArea <> "" Then GoTo ProjArea:
For ProjArea = 1 To 10000
If Cells(ProjArea, 3).Value = PR(PROJECT) Then
Top = ProjArea + 2
'MsgBox "OK"
End If
If Cells(ProjArea, 3).Value = "Grand Total" Then
'MsgBox "OK"
BOTTOM = ProjArea
Exit For
End If
ProjArea: Next ProjArea
'<<<<<<<<Move last week's data over to be sorted to new data later.>>>>>>>>
'MsgBox "OK"
'Range(Sheet1.Cells(Top, 39), Sheet1.Cells(BOTTOM, 42)).Value = Range(Sheet1.Cells(Top, 27), Sheet1.Cells(BOTTOM, 30)).Value
'Range(Sheet1.Cells(Top, 27), Sheet1.Cells(BOTTOM, 30)).ClearContents
'<<<<<<<<Establish VS group ranges within project on Layout Sheet.>>>>>>>>
VSGROUP = 0
For C = Top To BOTTOM
If Left(Cells(C, 2), 3) = PR(PROJECT) Then
VSGROUP = VSGROUP + 1
ReDim Preserve VS(VSGROUP)
VS(VSGROUP) = Right(Cells(C, 2), Len(Cells(C, 2)) - 4)
ReDim Preserve VSTop(VSGROUP)
VSTop(VSGROUP) = C + 1
End If
If Cells(C, 3) = "Total" Then
ReDim Preserve VSBottom(VSGROUP)
VSBottom(VSGROUP) = C - 1
Exit For
'MsgBox (VSBottom(VSGROUP) - VSTop(VSGROUP))
End If
Next C
'MsgBox "OK"
INSERT: If VSBottom(VSGROUP) - (VSTop(VSGROUP) - 1) < PivotVSQTY(VSGROUP) Then
Rows(VSBottom(VSGROUP) + 1).EntireRow.INSERT
VSBottom(VSGROUP) = VSBottom(VSGROUP) + 1
Rows(VSBottom(VSGROUP) - 1).EntireRow.Copy
Rows(VSBottom(VSGROUP)).EntireRow.Select
Selection.PasteSpecial Paste:=xlFormulas
Selection.PasteSpecial Paste:=xlFormats
Application.CutCopyMode = False
GoTo INSERT:
End If
SUBTRACT: If VSBottom(VSGROUP) - (VSTop(VSGROUP) - 1) > PivotVSQTY(VSGROUP) Then
Range(Cells(VSBottom(VSGROUP), 2), Cells(VSBottom(VSGROUP), 36)).Delete
VSBottom(VSGROUP) = VSBottom(VSGROUP) - 1
GoTo SUBTRACT:
End If
MsgBox "OK"
Range(Sheet1.Cells(VSTop(VSGROUP), 2), Sheet1.Cells(VSBottom(VSGROUP), 8)).Value = Range(PivotSheet.Cells(PivotVSTop(VSGROUP), 1), PivotSheet.Cells(PivotVSBottom(VSGROUP), 7)).Value
Range(Sheet1.Cells(VSTop(VSGROUP), 11), Sheet1.Cells(VSBottom(VSGROUP), 13)).Value = Range(PivotSheet.Cells(PivotVSTop(VSGROUP), 8), PivotSheet.Cells(PivotVSBottom(VSGROUP), 10)).Value
Range(Sheet1.Cells(VSTop(VSGROUP), 16), Sheet1.Cells(VSBottom(VSGROUP), 16)).Value = Range(PivotSheet.Cells(PivotVSTop(VSGROUP), 11), PivotSheet.Cells(PivotVSBottom(VSGROUP), 11)).Value
Range(Sheet1.Cells(VSTop(VSGROUP), 19), Sheet1.Cells(VSBottom(VSGROUP), 19)).Value = Range(PivotSheet.Cells(PivotVSTop(VSGROUP), 12), PivotSheet.Cells(PivotVSBottom(VSGROUP), 12)).Value
Range(Sheet1.Cells(VSTop(VSGROUP), 21), Sheet1.Cells(VSBottom(VSGROUP), 21)).Value = Range(PivotSheet.Cells(PivotVSTop(VSGROUP), 13), PivotSheet.Cells(PivotVSBottom(VSGROUP), 13)).Value
For TWK = VSTop(VSGROUP) To VSBottom(VSGROUP)
For LWK = BOTTOM To Top
If Sheet1.Cells(TWK, 2).Value = Sheet1.Cells(LWK, 39).Value Then
Range(Sheet1.Cells(TWK, 27), Sheet1.Cells(TWK, 30)).Value = Range(Sheet1.Cells(LWK, 39), Sheet1.Cells(LWK, 42)).Value
Range(Sheet1.Cells(LWK, 39), Sheet1.Cells(LWK, 42)).ClearContents
Exit For
End If
Next LWK
Next TWK
MsgBox "OK"
Next PROJECT
End Sub
------------------------------------------------
Sub NumberOfValueStreams()
'THIS SUB MUST BE RUN ON A "PIVOT" WORKSHEET!
ReDim PivotVS(X)
ReDim PivotVSQTY(X)
If Cells(3, 1) = "(blank)" Then Exit Sub
For VS = 3 To 100
If Cells(VS, 1) = "Grand Total" Then
ReDim Preserve PivotVSQTY(X)
LAST = VS
ReDim Preserve PivotVSBottom(X)
PivotVSBottom(X) = VS - 1
PivotVSQTY(X) = LAST - FIRST
Exit For
End If
If Cells(VS, 1) = "" Then GoTo VS:
A = 0
A: A = A + 1
If Mid(Cells(VS, 1), A, 3) <> "VS-" Then GoTo A:
B = 0
B: B = B + 1
If Len(Cells(VS, 1)) = Len(Mid(Cells(VS, 1), A, A + B + 1)) Then GoTo CarryOn:
If Mid(Cells(VS, 1), A + 2 + B, 1) <> " " Then GoTo B:
CarryOn:
NEWVS = Mid(Cells(VS, 1), A, A + B + 1)
If OLDVS = "" Then
FIRST = VS
X = 1
OLDVS = NEWVS
ReDim Preserve PivotVS(X)
PivotVS(X) = NEWVS
ReDim Preserve PivotVSTop(X)
PivotVSTop(X) = VS
End If
If NEWVS <> OLDVS Then
LAST = VS
ReDim Preserve PivotVSBottom(X)
PivotVSBottom(X) = VS - 1
ReDim Preserve PivotVSQTY(X)
PivotVSQTY(X) = LAST - FIRST
FIRST = VS
X = X + 1
ReDim Preserve PivotVS(X)
PivotVS(X) = NEWVS
ReDim Preserve PivotVSTop(X)
PivotVSTop(X) = VS
OLDVS = NEWVS
End If
VS: Next VS
'MsgBox "OK"
'Z = X
'For X = 1 To Z
'If PivotVS(X) <> "" Then MsgBox (PivotVS(X) & " " & PivotVSQTY(X) & " ROWS. TOP IS ROW " & PivotVSTop(X) & ". BOTTOM IS ROW " & PivotVSBottom(X) & ".")
'Next X
End Sub