In VBA (Excel 2010), I am
- dynamically creating a named range list
- using that list to create drop down choices in another column
When creating the drop down list, (a) using the named range doesn’t seem to work, and (b) if I don’t use the named range – and need to reference by sheet name and cell reference, I get into trouble because my sheet has just been renamed with today’s date.
This is messy, I know, but here is what I have so far:
' find the name of the worksheet and replace it with today's date
Dim vTabOriginalName As String
Dim vTabDateName As String
Dim vRangeName As String
vRangeName = "StageListChoices"
vTabOriginalName = ActiveSheet.Name
vTabDateName = Format(Now(), "yyyy-mmm-dd")
ActiveSheet.Name = vTabDateName
'create a drop down list for the stage (col K)
Range("AK3").Value = "NO ACTIVITY"
Range("AK4").Value = "SOLICITATION"
Range("AK5").Value = "OPPORTUNITY"
ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:=(vTabDateName & "!R3C37:R5C37")
'~~> Creates the list
With Range("K2:K" & vReportRowCount).Validation 'report row count known earlier
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=StageListChoices"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
The recorded macro when I created the named region made sense enough:
ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:= _
"=2013-JAN-24!R3C37:R14C37"
ActiveWorkbook.Names("StageListChoices").Comment = ""
Originally, I had been creating the drop down in VBA with a String variable, but the “real” list is 15 items long and I was getting errors upon reopening the file that the validation had been too long (?) and so had been turned off.
Basically, I’ve tried things like:
Formula1:="=StageListChoices"
Formula1:=vRangeName
Formula1:="=vRangeName"
Formula1:=vTabDateName & "!R3C37:R5C37"
Everything I’ve looked up says that the first one (Formula1:=”=StageListChoices”) should have worked – but it doesn’t.
Thank you!
Change
ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:=(vTabDateName & "!R3C37:R5C37")to
ActiveWorkbook.Names.Add Name:="StageListChoices", RefersToR1C1:=("='" & vTabDateName & "'!R3C37:R5C37")You were missing the
=and the'