Hi all,
So I got the code to work roughly how I intend it to but face two problems, one significantly more annoying than the other. Below is what I'm working with:
Sub Fill_Gaps_Spreadsheet()
' Interrupts Automatic Calculation, ScreenUpdating and StatusBar
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
' Converts column D to number
Range("D2:D70000").Select
With Selection
.NumberFormat = "General"
.Value = .Value
End With
' Imports Min where SOH is Nil for current range
With Range("H2:H70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=INDEX(CurrentMinMax!R2C4:R100000C4,MATCH(RC[-7]&RC[-4],CurrentMinMax!R2C1:R100000C1,0))"
End With
' Calculates Min Stock Value & SOH Value for current range
With Range("R2:R70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=RC[-1]*RC[-10]"
End With
With Range("T2:T70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=RC[-3]*RC[-9]"
End With
' Calculates Min Stock Value & SOH Value for start of year range
With Range("AW2:AW70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=RC[-1]*RC[-10]"
End With
With Range("AY2:AY70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=RC[-3]*RC[-9]"
End With
' Replaces current blank Unit Price with start of year Unit Price
With Range("Q2:Q70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=RC[31]"
End With
' Replaces start of year blank Unit Price with current range Unit Price
With Range("AV2:AV70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=RC[-31]"
End With
' Enter 0 for start of year SOH Nil
With Range("AP2:AP70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=0"
End With
' Imports Min level for start of year range
With Range("AM2:AM70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=INDEX(OldMinMax!R2C4:R129556C4,MATCH(RC[-38]&RC[-35],OldMinMax!R2C1:R129556C1,0))"
End With
' Import current range Min level where old min is not available
With Range("AM2:AM70000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeFormulas, 16).Select
On Error GoTo 0
Selection.FormulaR1C1 = "=RC[-31]"
End With
' Removes formulas from workbook (excluding BZ to CH)
Dim smallrng As Range
For Each smallrng In Range("H2:H70000,Q2:Q70000,R2:R70000,,T2:T70000,AM2:AM70000,AP2:AP70000,AV2:AV70000,AW2:AW70000,AY2:AY70000").Areas
With smallrng
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
Next smallrng
' Removes all created #N/As
Cells.Replace "#N/A", "", xlWhole
' Restores Automatic Calculation, ScreenUpdating and StatusBar
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
End Sub
My two problems are:
. I currently define the range of cells from row 2 to 70000 in most of the macro but the data actually stops at 61000 lines (may increase in the future though hence why I went with a bigger number). I therefore have 9000 lines of #N/A which I would like to be able to avoid but am worried the code would become a lot more complex.
. If I happen to run the macro twice (when blank cells have already been filled), some fields (eg column H addressed in the second argument of the macro) gets filled randomly with #N/A. Can this be prevented?
Thanks in advance for everyone's help.
S
Bookmarks