Macro button to open different workbooks depending on a cell value?

dplumlee12

New Member
Joined
Nov 13, 2012
Messages
15
Is there a way to use a cell value in one workbook to address a macro opening another workbook?

So in workbook Source.xlsm i have a cell that will have a value 1-10. Can I have a macro button that will open the appropriate destination workbook? Destination1.xlsm or Destination2.xlsm... depending on the cell value from source.xlsm?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Can be done. Are the workbooks you want to open in the same folder as the workbook that will contain the macro and button? If not, what is the file path to those workbooks?
 
Upvote 0
Yes the workbooks will be in the same folder.
Try this. Please note the comment pertaining to the cell address where you will have the workbook name. I'll leave it to you to assign the macro to a button on the sheet that contains the name of the workbook to be opened.
Code:
Sub OpenWorkbook()
'To be run from the workbook/sheet housing the number of workbook to open
'Assumes workbook name to open is in cell A1 - change to suit
Const sNam As String = "Dimension"
wbNam = sNam & Range("A1").Value & ".xlsm"  'Change range to suit
If Not WorkbookOpen(wbNam) Then
    Workbooks.Open FileName:=ThisWorkbook.Path & _
        Application.PathSeparator & wbNam
End If
End Sub
Function WorkbookOpen(WorkBookName As String) As Boolean
' returns TRUE if the workbook is open
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
WorkBookNotOpen:
End Function
 
Last edited:
Upvote 0
First I want to thank you for helping me with this.
When I run the code I am getting a compile error: ByRef argument type mismatch. I highlighted the section that the debugger is highlighting.

Try this. Please note the comment pertaining to the cell address where you will have the workbook name. I'll leave it to you to assign the macro to a button on the sheet that contains the name of the workbook to be opened.
Rich (BB code):
Sub OpenWorkbook()
'To be run from the workbook/sheet housing the number of workbook to open
'Assumes workbook name to open is in cell A1 - change to suit
Const sNam As String = "Dimension"
wbNam = sNam & Range("A1").Value & ".xlsm"  'Change range to suit
If Not WorkbookOpen(wbNam) Then
    Workbooks.Open FileName:=ThisWorkbook.Path & _
        Application.PathSeparator & wbNam
End If
End Sub
Function WorkbookOpen(WorkBookName As String) As Boolean
' returns TRUE if the workbook is open
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
WorkBookNotOpen:
End Function
 
Upvote 0
First I want to thank you for helping me with this.
When I run the code I am getting a compile error: ByRef argument type mismatch. I highlighted the section that the debugger is highlighting.
Add this line immediately after the line: Const sNam As String = "Dimension"

Dim wbNam as String
 
Upvote 0
Ok got past that point but getting another compile error due to the end sub in the middle of the code. Was I supposed to put that there?


Add this line immediately after the line: Const sNam As String = "Dimension"

Dim wbNam as String
 
Upvote 0
Ok got past that point but getting another compile error due to the end sub in the middle of the code. Was I supposed to put that there?
End Sub ends the subroutine. What's below that is a function used to see if the workbook you want to open is already open. In the VB editor there should be a line between End Sub and Function indicating that Excel recognizes the termination of one and start of the other. Hopefully, you copied both directly from the post and pasted them in one window. What exactly does the compile error say and what does it highlight?
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top