FORM MENU call workbook

User avatar
sal21
PlatinumLounger
Posts: 4351
Joined: 26 Apr 2010, 17:36

FORM MENU call workbook

Post by sal21 »

I have a form with many button named:
wb1
wb2
wb3
wb...
...

I need to assign for each button Wb, a command to aopen a excel workbook in c:\mydir\.
In effect button wb1 open the workbokk in c:\mydir\wb001.xls
... button wb2 open the workbokk in c:\mydir\wb2.xls
ecc...

How to?

but you should know that:

1) not possible to open a new workbook from the form until the last opned workbook not is closed.
2) all workbook have in Event Workbook Open a code to start other VBA for Excel code

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: FORM MENU call workbook

Post by Rudi »

See if this will help..

Code: Select all

Private Sub wb1_Click()
Dim wBook As Workbook
    On Error Resume Next
    Set wBook = Workbooks("wb001.xls")
    If wBook Is Nothing Then    'Not open
        Workbooks.Open "c:\mydir\wb001.xls"
    Else
        MsgBox wBook.Name & " is still open", vbExclamation
        Exit Sub
    End If
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FORM MENU call workbook

Post by HansV »

Sal, do you run this code from within Excel?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4351
Joined: 26 Apr 2010, 17:36

Re: FORM MENU call workbook

Post by sal21 »

HansV wrote:Sal, do you run this code from within Excel?
Sorry, yes in userform. I need to use VBA For Excel.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FORM MENU call workbook

Post by HansV »

You could use code like this:

Code: Select all

Private Const strPath = "C:\MyDir\"
Private wbk As Workbook

Private Sub wb1_Click()
    OpenWb "wb001.xls"
End Sub

Private Sub wb2_Click()
    OpenWb "wb002.xls"
End Sub

Sub OpenWb(strWorkbook As String)
    Dim wsh As Worksheet
    On Error Resume Next
    Set wsh = wbk.Worksheets(1)
    On Error GoTo 0
    If wsh Is Nothing Then
        Set wbk = Workbooks(strPath & strWorkbook)
    Else
        MsgBox "Close " & wbk.Name & " first", vbExclamation
    End If
End Sub
The declarations at the beginning must be at the top of the userform module.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4351
Joined: 26 Apr 2010, 17:36

Re: FORM MENU call workbook

Post by sal21 »

HansV wrote:You could use code like this:

Code: Select all

Private Const strPath = "C:\MyDir\"
Private wbk As Workbook

Private Sub wb1_Click()
    OpenWb "wb001.xls"
End Sub

Private Sub wb2_Click()
    OpenWb "wb002.xls"
End Sub

Sub OpenWb(strWorkbook As String)
    Dim wsh As Worksheet
    On Error Resume Next
    Set wsh = wbk.Worksheets(1)
    On Error GoTo 0
    If wsh Is Nothing Then
        Set wbk = Workbooks(strPath & strWorkbook)
    Else
        MsgBox "Close " & wbk.Name & " first", vbExclamation
    End If
End Sub
The declarations at the beginning must be at the top of the userform module.

ERROR!!! See image
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FORM MENU call workbook

Post by HansV »

Select Tools | Options... in the Visual Basic Editor.
Activate the General tab.
Make sure that "Break in Class Module" is selected in the "Error Trapping" section, and not "Break on All Errors".
S0847.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4351
Joined: 26 Apr 2010, 17:36

Re: FORM MENU call workbook

Post by sal21 »

HansV wrote:Select Tools | Options... in the Visual Basic Editor.
Activate the General tab.
Make sure that "Break in Class Module" is selected in the "Error Trapping" section, and not "Break on All Errors".
S0847.png
NOW ALL WORK PERFECT!
Tks.

Perpahs stupid question...
in the workbook i fill MyVar with "ok" is possible to return this value when i close the opened workbook end reuse for other code in the menu userform?

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FORM MENU call workbook

Post by HansV »

If you have a variable in a workbook and then close that workbook, that variable won't be available any more. You can only refer to variables in open workbooks.
Best wishes,
Hans

User avatar
StuartR
Administrator
Posts: 12601
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: FORM MENU call workbook

Post by StuartR »

I use CustomDocumentProperties to store values that I want avaialble in VBA even after the workbook has been closed and reopened.
StuartR


User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: FORM MENU call workbook

Post by Rudi »

That's actually an interesting thought.
They are just text fields and you can write anything to them (obviously only those not in use at the time).

Only backfires when someone comes along who uses these properties, opens the dialog and sees, Company Name: 56 :laugh:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.