Excel friends,
I would like to assign names to some specific cell references located within a range using a version of this code. It currently does not assign the names correctly. Can somebody help me by fixing it. Thanks!
I would like to assign names to some specific cell references located within a range using a version of this code. It currently does not assign the names correctly. Can somebody help me by fixing it. Thanks!
Code:
Sub AssignNametoRefs()
Dim Rgn As Range
Dim RefNames As String
Dim LblSheet As String
RefNames = "B3:C5"
LblSheet = "Sheet1"
Rgn = Worksheets(LblSheet).Range(RefNames)
'Column B = Names Example: Prices
'Column C = Cell References Example: P5:P20
'So, the range P5:P20 is named as Prices
For i = 1 To UBound(Rgn)
If 1 Then '1=Workbook Scope / 0 = Worksheet Scope
'Single Cell Reference (***Workbook Scope***)
ThisWorkbook.Names.Add Name:=Rgn(i, 1), RefersTo:=Rgn(i, 2)
Else
'Single Cell Reference (***Worksheet Scope***)
Worksheets(LblSheet).Names.Add Name:=Rgn(i, 1), RefersTo:=Rgn(i, 2)
End If
Debug.Print Rgn(i, 1) & "-" & Rgn(i, 2)
Next i
End Sub