I have a routine that adds new customer ranges to an estimate and actual sheet along with validation, formulas and protection. I add named ranges to certain cells as they are used throughout the sheets in formulas.
The code part that names the ranges is as follows (it works, the ranges are named correctly as seen in name manager)
The function that does the name ranging is declared as so
Function InitializeAddedRange(rangeToFormat As Range, _
custName As String, currentSheet As Worksheet)
and is called as follows
Call InitializeAddedRange(startCell, customerName, sourceWorksheet)
so in the snippet below rangeToFormat is startCell
Set rangeToFormat = rangeToFormat.Offset(0, 1)
If currentSheet.Name = "Actual Quarterly Sales" Then
rangeToFormat.Name = Replace(custName, " ", "") & "ActualBasePrice"
rangeToFormat.Offset(0, 1).Name = Replace(custName, " ", "") & "ActualIsCurrent"
ElseIf currentSheet.Name = "Quarterly Sales Projections" Then
rangeToFormat.Name = Replace(custName, " ", "") & "BasePrice"
rangeToFormat.Offset(0, 1).Name = Replace(custName, " ", "") & "IsCurrent"
End If
In a function that is called in the same subroutine but AFTER the code above, I am trying to retrieve the name of the range as its required in numerous formulas. (startCell.Name)
Set tempRange = Range(startCell.Offset(1, 3), startCell.Offset(1, 14))
For Each cCell In tempRange
With cCell
.Value = 0
.NumberFormat = "$#,##0"
.FormulaR1C1 = "=DrumGallons*" & startCell.Name & "*R[-1]C"
.Locked = True
End With
Next cCell
The problem is startCell.Name
returns the sheetname and cell address i.e. SalesEstimates$B$12 instead of the named range which should be customernameBasePrice. I've done a lot of VBA work and I can't recall ever coming across this problem. Does this occur because the subroutine hasn't finished and the named range actually hasn't changed within excel yet before the function is trying to access it? I don't think that can be it but I've tried numerous things none of which work.