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
FORM MENU call workbook
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: FORM MENU call workbook
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.
Rudi
If your absence does not affect them, your presence didn't matter.
-
- Administrator
- Posts: 78412
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4351
- Joined: 26 Apr 2010, 17:36
Re: FORM MENU call workbook
Sorry, yes in userform. I need to use VBA For Excel.HansV wrote:Sal, do you run this code from within Excel?
-
- Administrator
- Posts: 78412
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: FORM MENU call workbook
You could use code like this:
The declarations at the beginning must be at the top of the userform module.
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
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4351
- Joined: 26 Apr 2010, 17:36
Re: FORM MENU call workbook
HansV wrote:You could use code like this:
The declarations at the beginning must be at the top of the userform module.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
ERROR!!! See image
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 78412
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: FORM MENU call workbook
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".
Activate the General tab.
Make sure that "Break in Class Module" is selected in the "Error Trapping" section, and not "Break on All Errors".
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4351
- Joined: 26 Apr 2010, 17:36
Re: FORM MENU call workbook
NOW ALL WORK PERFECT!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".
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?
-
- Administrator
- Posts: 78412
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: FORM MENU call workbook
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
Hans
-
- Administrator
- Posts: 12601
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: FORM MENU call workbook
I use CustomDocumentProperties to store values that I want avaialble in VBA even after the workbook has been closed and reopened.
StuartR
-
- gamma jay
- Posts: 25455
- Joined: 17 Mar 2010, 17:33
- Location: Cape Town
Re: FORM MENU call workbook
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
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
Regards,
Rudi
If your absence does not affect them, your presence didn't matter.
Rudi
If your absence does not affect them, your presence didn't matter.