My friend shared a .bas file with me and told me to save it as .xlam in the vbEditor to have it as an Addin.
I've browsed in Add-ins and am able to enable it in my workbook.
Is there a way I can assign a keyboard shortcut to the Add-in? There's only one sub in that add-in file now.
I tried writing another sub with
Application.onKey "+^{C}" ,'Calculate'
But it doesn't trigger the sub to be executed.
You don't assign a shortcut to an add-in. Rather, you assign a shortcut to a macro - that is, a Public Sub procedure in your standard module.
So your code file might look like this:
Option Explicit
Public Sub Calculate()
'...code...
End Sub
Open it in Notepad. I'll look like this:
Attribute VB_Name = "Module1"
Option Explicit
Public Sub Calculate()
'...code...
End Sub
Under Public Sub Calculate(), you want to add an attribute so that the file looks like this:
Attribute VB_Name = "Module1"
Option Explicit
Public Sub Calculate()
Attribute Calculate.VB_ProcData.VB_Invoke_Func = "C\n14"
'...code...
End Sub
This is exactly how Excel's macro recorder assigns macro hotkeys: no need for any Application.OnKey work-arounds.
Save the file, import it into your VBA project: Ctrl+Shift+C will now invoke that macro.
If you're using Rubberduck, forget all of the above and just go to your module in the VBA editor, find the procedure and annotate it like so:
Option Explicit
'#ExcelHotkey("C")
Public Sub Calculate()
'...code...
End Sub
Where "C" will make the hotkey Ctrl+Shift+C; I'd warmly recommend not using "c" to avoid hijacking Ctrl+C (Copy).
Bring up the code inspections toolwindow, hit the "refresh" button; under "Rubberduck Opportunities" there should be an inspection result warning about annotations & attributes being out of sync - select "add member attribute" from the "Fix" menu, and you're done - no need to export/edit/import or deal with any obscure syntax, and if you want to change the hotkey, simply edit the comment accordingly and re-synchronize annotations & attributes.
See VB_Attribute annotations for more information.
As for saving your VBA project as an add-in, simply save your VBA project's host workbook as a .xlam add-in file, then close Excel altogether and re-open it - load your .xlam from the Developer Ribbon tab's "Excel Add-ins" button (hit "Browse" to locate your .xlam file if it's not in the list).
Related
I'm creating an excel module to export any table inside a workbook as .csv
My module generate a UserForm where the user can chose which table to export inside a combobox and then click 'export'
Everything is working perfectly, but I have to create a button in my workbook and call my module on click.
However I want to keep it very simple to setup inside any workbook by just import the module and run it with a keyboard shortcut '''Ctrl+R''' for example.
I search a lot about that but I never find something, Is it something possible?
Yes you can what you want to. First import your module to your new excel workbook and then assign keyboard shortcut to Workbook_Open event by copying code to ThisWorkbook area.
Sub MyMacro()
MsgBox ("ok")
End Sub
In ThisWorkbook area (which is in Microsoft Excel Objects / Project Window)
Private Sub Workbook_Open()
Application.MacroOptions Macro:="MyMacro", Description:="", ShortcutKey:= _
"a"
End Sub
This code assign Ctrl+"Defined_Letter" shortcut to your MyMacro macro. In this case, the code's shortcut is [Ctrl+a].
I have a series of document templates created in Excel macro-enabled workbooks with an array of different macros coded in vba. I'm fairly new to vba, but I was able to cobble together code from several different sources that mostly achieved what I was looking to do: create a customized tab order, which also incorporates ActiveX Comboboxes.
For the most part, all code in each modules works great on the respective files saved locally on my machine. However, if these files are Emailed or saved on an intranet site or somewhere where they must be downloaded by end-users, something squirrelly happens that I haven't been able to sort out. When the first file is opened, the user will receive Excel's "Protected View" prompt, requiring that they click the "Enable Editing" box to access/modify the file. This is fine and the user can manipulate the first xlsm file without issue. However, with that first xlsm file open, if they additionally open any of the other xlsm workbooks that I've created (with ostensibly identical code), they are not prompted with the "Protected View" message and the following happens in that second workbook:
The cell focus is redirected to cell A1 instead of whichever cell should have been selected when the second workbook was opened,
The customized tab order I've delineated is disabled,
Both the horizontal and vertical scrollbars vanish from view, and
All tabs for each sheet in the second workbook are no longer visible.
I've tried manipulating and rewriting the code in each of these workbooks every which way I could think of, but I can't figure out what could be the culprit causing the above issues. I'd wager it has something to do with how Excel is initializing the second workbook when it's been downloaded, and that it has more to do with the code in the ThisWorkbook module -- here are the snippets that I think are relevant:
Private Sub DisableTabbing()
If Not (Application.ActiveProtectedViewWindow Is Nothing)
Then Application.ActiveProtectedViewWindow.Edit
End If
Application.OnKey "{TAB}"
Application.OnKey "+{TAB}"
Application.OnKey "~"
Application.OnKey "+~"
Application.OnKey "{ENTER}"
Application.OnKey "+{ENTER}"
End Sub
Private Sub Workbook_Activate()
EnableTabbing
End Sub
Private Sub Workbook_Deactivate()
DisableTabbing
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
EnableTabbing
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
DisableTabbing
End Sub
On my machine, I don't encounter this at all when any two workbooks are locally saved and opened, but only when the files are Emailed/downloaded. I'm sure there's a relatively simple fix for this, but I'm not as experienced with VBA, so I'm hoping someone here might be able to point me in the right direction. Is there something in the code that could be causing that, or better yet, some additional code that I could insert that would prevent it from happening?
I want to open only the userform when I open the excel sheet. I used the below code but other excel also become invisible. I want to display other open excel file and only macro contained file should be disable.
Application.Visible = False
UserForm4.Show
If I see well what you mean, I think the best thing to do is to make your xlsm file an add-in. In order to do this:
1) Call the UserForm4.Show into the Workbook_Open event of your .xlsm;
2) Save your workbook as Excel Add-In (extensions: .xla or the newer .xlam);
3) Enable the add-in on Excel, so every time that a workbook is opened you can show the form and not the entire workbook, so avoiding also to let the EXCEL.EXE instance open (that will remain so if the Application is Visible=False, because no user would see it after closing the workbook they're working with).
Add-ins have a great power in terms of use and distribution, I suggest you to get started from here and go deeper into this very nice tool.
Add the following code to the user form...
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
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
I have created a excel macro, now i want it distribute to others who need to use it.
All i want is if some one could please help me with steps to attach the macro to a custom tool bar button and then save both the custom tool bar and to .xla file.
Then the users can store the .xla file on to XLSTART director and when excel is launched custom tool bar appears and readyto use.
This code adds a new menu option, and references VBA methods, so is similiar to what you want.
Add these to the workbook VBA:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
MenuBars(xlWorksheet).Menus("NewMenu").Delete
End Sub
Private Sub Workbook_Open()
On Error Resume Next
MenuBars(xlWorksheet).Menus("NewMenu").Delete
Dim statMenu As Menu
Set statMenu = MenuBars(xlWorksheet).Menus.Add(Caption:="NewMenu")
statMenu.MenuItems.Add Caption:="Item 1", OnAction:="RunFirstItem"
statMenu.MenuItems.Add Caption:="Item 2", OnAction:="RunSecondItem"
End Sub
Then add some methods RunFirstItem & RunSecondItem to the module code.
Save as an .XLA and off you go.