Can you open an external wb, retrieve a drop down menu then close it

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
1,039
I have a wb with a drop down menu that correctly references an external wb to get the list but the external wb needs to be open to do so. It is going to be used by more than one person and will be stored on a server. Is there a way to have the external wb open when the cell with the list is selected, retrieve the drop down list then close it? I can't have it open all the time as it stores additional information that could be added to by another user and might be accidentally saved over.

Thanks
 

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
1,039
I thought of a possible solution but I don't know if it will work or how to get it to work.

The idea was that I put code into the change event for the worksheet:
Code:
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("B7")) Is Nothing Then
            Workbooks.Open ThisWorkbook.Path & "\" & "Client_list.xlsm"
        End If
    End If
The client_list file is stored in the same folder and the drop down list is in B7, this code wouldn't open the file upon selection of B7. Could someone help me with the vba please?
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,261
Try this combination of event macros

Code:
[COLOR=#006400]Private Sub Worksheet_SelectionChange[/COLOR](ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("B7")) Is Nothing Then OpenClientList
    End If
[COLOR=#006400]End Sub[/COLOR]
[COLOR=#006400]Private Sub Worksheet_Chang[/COLOR]e(ByVal Target As Range)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("B7")) Is Nothing Then CloseClientList
    End If
[COLOR=#006400]End Sub[/COLOR]
[COLOR=#006400]Private Sub Worksheet_BeforeRightClick[/COLOR](ByVal Target As Range, Cancel As Boolean)
    If Selection.Count = 1 Then
        If Not Intersect(Target, Range("B7")) Is Nothing Then
            Cancel = True
            CloseClientList
        End If
    End If
[COLOR=#006400]End Sub[/COLOR]
[COLOR=#006400]Private Sub OpenClientList[/COLOR]()
    On Error Resume Next
    Workbooks.Open ThisWorkbook.Path & "\" & "Client_list.xlsm"
    Me.Parent.Activate
[COLOR=#006400]End Sub[/COLOR]
[COLOR=#006400]Private Sub CloseClientList[/COLOR]()
    On Error Resume Next
    Application.ScreenUpdating = False
    Workbooks("Client_list.xlsm").Close False
[COLOR=#006400]End Sub[/COLOR]
Explanation
Selecting the cell opens the workbook
Changing the value closes the workbook
Before Right Click event prevents right-clickng the cell triggering the selection macro and gives a method to close the workbook if value in cell is not amended
I prefer event macros to be "clean" of detail
- may also want to use them for other things later and they become very cluttered
- hence separate macros for opening and cloing the workbooks
On Error Resume Next prevents VBA failing if workbook not found


Q Is the data validation list derived from a static range of cells in Client_List ?
 
Last edited:

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,261
If the data validation list is derived from a static range of cells in Client_List ...
... consider using a formula to get the values from Client_List as illustrated below

Excel 2016 (Windows) 32 bit
A
B
1
Formula in A2 copied down
2
Ant ='C:\Folder\SubFolder\[Client_list.xlsm]MySheet'!$A1
3
Cat
4
Dog
5
Mouse
6
Hampster
7
Cow
8
Sheep
9
Lamb
10
Goat
11
Elephant
Sheet: Sheet2
 

Forum statistics

Threads
1,078,232
Messages
5,338,997
Members
399,274
Latest member
WilliamWavehill

Some videos you may like

This Week's Hot Topics

Top