I'm trying to put a dropdown menu in an Excel worksheet, with options from another worksheet, in another workbook. I'm using VBA because I want to customize those options, based on another dropdown menu. I managed to do it by naming the range in the source sheet, but that file is read-only so I can't save the name to automate the process. So I tried to use only the column name in the reference (like $B:$B, I only need the B column), but when I run the macro it stops with Run-time error 1004.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("H6")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Call main
End If
End Sub
Private Sub main()
reason = Range("H6").Value
List = ""
If reason = "Project ID/Task ID" Then
List = "=PIDTID"
Else
List = "=Cost_Center"
End If
With Range("I6").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=List
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
This is my macro, as you can see, main()
is called when H6 is changed, because that is where my other dropdown menu is. Then, based on the value from that cell, List gets assigned a value equal to the defined name that refers to the range in the other worksheet.
As an example, if PIDTID name is equal to ='[20190812_WP_and_CostCenters_Responsible.xls]Projects responsibles'!WBSName
, the script works. Note that WBSName is the name given to column B in the source sheet.
If I set PIDTID to ='[20190812_WP_and_CostCenters_Responsible.xls]Projects responsibles'!$B:$B
I will get the error on line
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=List
Cost_Center
name acts the same.
I want to mention that these tests were done having the source file opened. If I tried with it being closed, I would get the same error.
Is there a way to get rid of the error, or another way by which I can refer to cells in sheets of other workbooks without naming them? I would also like to work without having to open the source file. Thank you!