I am running a spreadsheet that has a macro. Within this macro there is a Userform. This userform has a button that when pressed opens the print preview of data in a spreadsheet. When I execute the "PrintPreview" method, all buttons the page setup tools are disabled, as can be seen in the image below.
Below is the code I'm running
Private Sub Workbook_Open()
UserForm1.Show
End Sub
The code below is inside UserForm1
Private Sub CommandButton1_Click()
Dim MySheet As Worksheet
Application.ScreenUpdating = False
Set MySheet = Worksheets("Plan3")
MySheet.Select
MySheet.Range("$A1:$AI166").Select
MySheet.PageSetup.PrintArea = "$A$1:$AI$166"
ActiveWindow.View = xlNormalView
Application.ScreenUpdating = True
Me.Hide
MySheet.PrintPreview
Me.Show
End sub
I couldn't find the reason why the Page Setup Tools buttons is disabled. Can someone help me?
Note 1: This code is inside a userform. The userform is loaded into the Workbook_Open() event.
Note 2: When I open the spreadsheet by double clicking on the file, the userform is loaded automatically. Clicking the CommandButton1 button displays the print preview screen, however the Page Setup Tools buttons is disabled.
If I open the Visual Basic editor and run the Workbook_Open(), clicking the CommandButton1 button shows the print preview screen and the Page Setup Tools buttons are enabled. That is, if the macro runs with the Visual Basic editor open the problem does not happen.
Note 3: This is only happening in the 2007 version of excel.
Note 4: I did a test by opening UserForm1 in "(vbModeless)" mode. In this case the problem did not occur. However in the application I am developing, I need to open the userform in "(vbModal)" mode.
Related
Here the context : I'm working on a local workbook (c:) that directly opens a UserForm within the Workbook_Open() Sub. Then, when UserForm is closed, Excel is also automatically closed. Nothing special there.
The problem : Even with a correct setup in my Trust Center Settings (Disable all macros WITH notification), the warning security message pops up again each time I reload that workbook.
Here my code :
Private Sub Workbook_Open()
' modal display of my form
myUserForm.Show vbModal
' upon return from my form, save and close that workbook
Me.Close savechanges:=True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' also quit Excel app if no more workbook is opened
If Application.Workbooks.Count = 1 Then Application.Quit
End Sub
I'm pretty sure that's related to the fact that Excel is automatically closed by program, and not by user himself.
For example if I reduce the code to the strict minimum, like :
Private Sub Workbook_Open()
myUserForm.Show vbModal ' modal display of my form
End Sub
In that situation, so without any Close or Quit, I just fall into my main worksheet when the UserForm is closed, then I just manually close Excel, with or without saving, and that's it, warning popup will not appear next time I'll open that workbook.
I don't know if there is a solution without playing with Trust Locations or All macros enabled.
Any ideas ?
Thanks for your help
I have created a Userform, currently I am using this code to hide Excel and just show the userform (Code was place in Workbook)
Private Sub Workbook_Open()
Application.Visible = False
ricwin.Show
End Sub
The issue I am facing is that in ocassions the excel file will open but not as macro enable, and I require to close it and reopen it so the macro runs.
Also is there a chance to display a taskbar icon for the userform without an excel file being in background?
You can fix the problem by adding a ToggleButton to the userform. When the workbook is opened, the worksheet is hidden, only the userform is shown. For this, the following codes are added to a module:
Sub Auto_Open()
Application.Visible = False
UserForm1.Show
UserForm1.ToggleButton1.Value = False
End Sub
enter image description here
Source of sample file
I'm trying to automatically display the IDE when Excel is launched.
Is there a way to simulate a click on the "Visual Basic Editor" icon in the ribbon? I looked into Application.CommandBars but there's nothing about the Ribbon.
Private Sub Workbook_Open()
' Display Visual Basic Editor
End Sub
The Commandbars object has an ExecuteMso method that allows you to "push" any Ribbon button, so:
Application.CommandBars.ExecuteMso ("VisualBasic")
As noted by Comintern, the Application qualification is necessary when using this in a Workbook_Open event, otherwise you'll get an error 91.
To find the mso, go into the Quick Access toolbar's Ribbon menu, find what you want and hover:
It would be
Private Sub Workbook_Open()
' Display Visual Basic Editor
Application.VBE.MainWindow.Visible = True
End Sub
If you get Programmatic Access error:
Programmatic Access To Visual Basic Project Is Not Trusted - Excel
I have a fully functional Macro that I'm trying to convert to an Add-In so I can use it on all excel documents. However I can't get it create a menu. Please Help.
First Things First:
I'm using Office 2011-Excel on a Mac running Mountain Lion
My .xlam file is saved in the correct folder Applications/Microsoft Office 2011/Office/Add-Ins
I've installed it correctly using the Tools/Add-Ins menu
My code is in the This Worksheet section of the .xlam file
My Add-In's source code is viewable from all documents
My code as follows:
Option Explicit
Dim cControl As CommandBarButton
Private Sub Workbook_Open()
On Error Resume Next 'Just in case
Application.CommandBars("Worksheet Menu Bar").Controls("P Wave").Delete 'Delete any existing menu item that may have been left.
Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add 'Add the new menu item and Set a CommandBarButton Variable to it
With cControl 'Work with the Variable
.Caption = "P Wave"
.Style = msoButtonCaption
.OnAction = "runSheet()"
'Macro stored in a Standard Module
End With
On Error GoTo 0
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'In case it has already gone.
Application.CommandBars("Worksheet Menu Bar").Controls("P Wave").Delete
On Error GoTo 0
End Sub
I can't get my Macro menu to appear. Please help
Add your command buttons, or even your own Tab, on the Ribbon although you may have to learn a bit about RibbonX XML. It will give compatibility back to Excel 2007 (I think).
This link may get you started.
Also, This visual designer may be more exciting to get you going. I haven't used it but Andy Pope is the man on all things VBA!
How do i set a button to run my program, what i wanna do, i don't want my this workbook to run when i open up a my workbook, i create a button and i want my button to run macros when i click it and if i don't click it then my macros should not run and my workbook should stay it is, its kind a like enable macros content or disable content but want to do it with button
sub button_click()
application.enableevents = false
application.enableevents = true
end sub
I think you shoukd do a little more research before posting question. This link details how to add a button and assign a macro to it in excel vba.
http://office.microsoft.com/en-gb/excel-help/add-a-button-and-assign-a-macro-to-it-in-a-worksheet-HP010236676.aspx