how do i set button to run macros in vba - excel

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

Related

Excel Error when using button made from Custom UI Editor- Cannot run the macro "***". The macros may not be available in this workbook or all macros

I built a series of macros, all of which have worked with the custom ribbon I built using the Custom UI Editor.
I added one more macro and updated my Ribbon with a new button. I test the macro first and it works when run from the developer tab or just running directly from VBA and the sub heading is as follows: "Sub GetDES()".
Once I make it so it works with the button on the ribbon by changing to:
Sub GetDES(control as IRibboncontrol)
I get the "Cannot run macro..." error
Sub GetDES(control as IRibboncontrol)
'PURPOSE: Get description page and insert into each confirm page
Dim Fpath As String
Fpath = ActiveSheet.Range("I6").Value
With Application.FileDialog(msoFileDialogFilePicker)
.ButtonName = "Add this DES!"
.AllowMultiSelect = False
.InitialFileName = Fpath
.Filters.Add "All Pictures", "*.*"
.Show
If .Show = -1 Then
Dim img As Object
Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))
img.Left = 75
img.Top = shOutput.Range("B40").Top
img.Width = 450
Else
MsgBox ("Cancelled.")
End If
End With
End Sub
The only thing I can think of is I still have something weird happening when I run it without the Control. When I run it the dialouge picker opens twice. I'm wondering if that is my problem with running it from the button on the ribbon.
When I step through the code, the line
set img=Activesheet.Picture.Insert(.SelectedItems(1))
is what causes the file dialogue picker to open again.
Ideas? Thank you
To make sure the callback is invoked by the Office you may try to assign a sample sub to a button, for example, with a simple MsgBox statement to make sure it works correctly.
Sub GetDES(control as IRibbonControl)
MsgBox "test"
End Sub
If that works correctly you can try putting your original code. But you may try to avoid shorthand properties like ActiveSheet or With operator. Hope it helps.
I believe I stumbled across my issue. I built a new module that I wrote this code in, I named the module with the same name as the sub. When Testing, I never went to run it from the macro list, I just ran it from the VBA window. When I looked at the Macro's List window, I saw that the new macro I wrote in this workbook was not displaying like the other macros. As you see in the photo, "Confirm Tool-Master_V5.xlsm'!GetDES.GetDES"
I removed that module and took the copy, listed as GetDES3 and renamed it
GetDES(control as IRibboncontrol)
and it runs from my ribbon. I'm not sure why it looked different or if there was something to do with the module name and the sub being named the same that caused it to be like that. so I tested it. I wrote a test sub and named the module test and it did the same thing.
So, Lesson is, don't name your sub and the module with the same name

How to know where a button is located

I have a macro, this macro can be used from different worksheets through a button so it can update my data.
What I want to do is put at the end of the macro something that says "Now go back to the worksheet from where the button was pressed"
Is there a way to know which worksheet I was on when I pressed the button?
I have found a way to do this.
I can put this at the very beginning of my code
active_sheet = ActiveSheet.Name
and then at the very end do this
Worksheets(active_sheet).Activate

Page Setup Tools is Disabled - PrintPreview

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.

Why additional excel window appears

Scenario
I am using a macro whereby I use Application.Visible = False to hide the workbooks. Also I use Application.Visible = True to unhide the workbook. At certain situation, I use Windows(ThisWorkbook.Name).Visible = False and Windows(ThisWorkbook.Name).Visible = True to hide and unhide only the workbook which contains macro.
Problem
I noticed during these operations, some additional excel windows(without any workbook) appear other than the workbook. Please see the picture below. You can see a grey window behind with a name Excel. That is the window I am talking about
If I closed that window, the whole excel will close. Does anyone know why this extra window appearing and how to prevent it from appearing?
I am not sure if this will meet the needs of your specific situation. But, what if you kept Application.Visible = False at the beginning of your code and changed Application.Visible = True to
Application.Windows(ThisWorkbook.Name).Visible = True at the end. This worked for me.
Hiding Excel
With the following code
Sub AppVisibleTrue
Application.Visible = True
End Sub
Sub AppVisibleFalse
Application.Visible = False
End Sub
you are showing or 'hiding' the 'whole' excel application, so you have to 'unhide' it in the same code, otherwise you won't be able to use the open files after you hide it, e.g. open a new workbook, in VBE add a new module and paste the above code into the module. Now, stay in VBE!!! Run the 'False' Sub. You will notice Excel has 'vanished', but you can still find it in the Task Manager's processes. Now run the 'True' Sub. Excel has 'reappeared'.
The following process will make Excel 'vanish'. The only way to close it will be via the Task Manager. If you not too familiar with doing this, just take my word for it.
Close the VBE. Now run the 'False' Sub. Excel has 'vanished'.
To conclude, this is obviously an error in your code, so I would suggest if you want to show a window (worksheet) that isn't 'ThisWorkbook' and you're dancing from one to the other, you should declare a variable
Const strSheet as String = "Sheet2"
Dim oSheet as Worksheet
'...
Set oSheet = ActiveWorkbook.Worksheets(strSheet)
Now you do with oSheet whatever you want.
Try to lose Active, Select and similar methods in the code.
If you would provide the actual scenario with the codes, a better assessment of the problem could be done.
The additional Excel window(s) could be related to your Windows Explorer. If you are previewing a document (in this case an Excel document) then the application to view that document is running in the background.
In this case, forcing the application to be visible will also make the background "preview" windows visible.

How can I tie a VBA macro to a button in Excel

I have created a macro and a button but I can't see where to edit the button setting to invoke the macro. I'm using Excel 2003.
If you've used a Form Control. The chance to assign a macro will come up right after you add the button to the sheet. Or, if the button is already on the sheet, you can right-click on it and select Assign Macro.
If you've used an ActiveXControl, right-click on the button and select View Code. This will open up the VBE with
Private Sub myCommandButtonName_Click()
End Sub
From this sub call your macro like so...
Private Sub myCommandButtonName_Click()
Call myMacro
End Sub

Resources