Greetings,
The code below works correctly on certain sheets. The code is supposed to calculate the percent change from 1990 to 2012 and from 2005 to 2012, and put the calculations on the 4th and 5th row from the last non-empty row, respectively. All the sheets are identical except for 3. The sheets that are different only have a different number of years of data. For some reason this causes the macro to put the percent change calculations in random rows below the correct location. Also, the macro doesn't work correctly on one of the identical sheets. I have no idea why.
Any thoughts as to what might be the problem? Thank you very much for your help!
The code below works correctly on certain sheets. The code is supposed to calculate the percent change from 1990 to 2012 and from 2005 to 2012, and put the calculations on the 4th and 5th row from the last non-empty row, respectively. All the sheets are identical except for 3. The sheets that are different only have a different number of years of data. For some reason this causes the macro to put the percent change calculations in random rows below the correct location. Also, the macro doesn't work correctly on one of the identical sheets. I have no idea why.
Code:
Sub PercentChangeCalPIP()
Dim iRow As Long
Dim sheet_name As Range
For Each sheet_name In Sheets("WS").Range("I:I")
If sheet_name.Value = "" Then
Exit For
Else
With Sheets(sheet_name.Value)
'90-20## Percent Change
iRow = 14
Cell_val = .Cells(iRow, 1)
While Cell_val <> ""
iRow = iRow + 3
Cell_val = .Cells(iRow, 1)
Wend
.Cells(iRow + 3, 1) = "1990-2012"
'Column B,2
Cell_val = .Cells(iRow, 2)
While Cell_val <> ""
iRow = iRow + 3
Cell_val = .Cells(iRow, 2)
Wend
.Cells(iRow + 3, 2) = "=((B14/VLOOKUP(1990, A14:B46, 2,FALSE))-1)"
'Column C,3
Cell_val = .Cells(iRow, 3)
While Cell_val <> ""
iRow = iRow + 3
Cell_val = .Cells(iRow, 3)
Wend
.Cells(iRow + 3, 3) = "=((C14/VLOOKUP(1990, A14:C46, 3,FALSE))-1)"
'Column D,4
Cell_val = .Cells(iRow, 4)
While Cell_val <> ""
iRow = iRow + 3
Cell_val = .Cells(iRow, 4)
Wend
.Cells(iRow + 3, 4) = "=((D14/VLOOKUP(1990, A14:D46, 4,FALSE))-1)"
'Column E,5
Cell_val = .Cells(iRow, 5)
While Cell_val <> ""
iRow = iRow + 3
Cell_val = .Cells(iRow, 5)
Wend
.Cells(iRow + 3, 5) = "=((E14/VLOOKUP(1990, A14:E46, 5,FALSE))-1)"
'05-20## Percent Changes
Cell_val = .Cells(iRow, 1)
While Cell_val <> ""
iRow = iRow + 4
Cell_val = .Cells(iRow, 1)
Wend
.Cells(iRow + 4, 1) = "2005-2012"
'Column B,2
Cell_val = .Cells(iRow, 2)
While Cell_val <> ""
iRow = iRow + 4
Cell_val = .Cells(iRow, 2)
Wend
.Cells(iRow + 4, 2) = "=((B14/VLOOKUP(2005, A14:B46, 2,FALSE))-1)"
'Column C,3
Cell_val = .Cells(iRow, 3)
While Cell_val <> ""
iRow = iRow + 4
Cell_val = .Cells(iRow, 3)
Wend
.Cells(iRow + 4, 3) = "=((C14/VLOOKUP(2005, A14:C46, 3,FALSE))-1)"
'Column D,4
Cell_val = .Cells(iRow, 4)
While Cell_val <> ""
iRow = iRow + 4
Cell_val = .Cells(iRow, 4)
Wend
.Cells(iRow + 4, 4) = "=((D14/VLOOKUP(2005, A14:D46, 4,FALSE))-1)"
'Column E,5
Cell_val = .Cells(iRow, 5)
While Cell_val <> ""
iRow = iRow + 4
Cell_val = .Cells(iRow, 5)
Wend
.Cells(iRow + 4, 5) = "=((E14/VLOOKUP(2005, A14:E46, 5,FALSE))-1)"
End With
End If
Next sheet_name
End Sub
Any thoughts as to what might be the problem? Thank you very much for your help!