Customizing a ribbon with VBA in Excel - vba

I've already learned that creating a custom tab is in Excel not possible in this! post (unlike i.e. MSProject)
Specifically, can I change the paths of the macros to the current location ?
Edit
It seems that this page may lead to the answer, but I still don't know how, though
Some more in-detail description of the situation:
The user will download a file, which contains a list of materials, equipment, labor. Each column contains information about quantities, pricing etc
When the user click on this button I want to create (I have created manually), a macro called 'Main' in another workbook launches and copies the whole sheet (the contents) to itself and performs some things that procedures do, on it.
So the problem I'm facing, is twhen I'm sending a new version to the client, he has to put it in the exact location or it won't work. Since there's a mix of Mac and Windows computers involved, I'd rather see a situation where the button is assigned to the procedure when the user opens WorkBook B (the one that contains the code).
This way, a new version has to be openened only once, and then for continuous use, the user can just open the downloaded file, click the appropriate button and WorkBook B will open itself and execute.
Maybe there's other ways to go about this. I haven't checked if it's not easier to assign a button to the quick access toolbar...

This is some code I use to add a custom toolbar:
Set cbToolbar = Application.CommandBars.Add(csToolbarName, msoBarTop, False, True)
With cbToolbar
Set ctButton1 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton2 = .Controls.Add(Type:=msoControlButton, ID:=2950)
Set ctButton3 = .Controls.Add(Type:=msoControlButton, ID:=2950)
End With
With ctButton1
.Style = msoButtonIconAndCaption
.Caption = "Set &Picklists"
.FaceId = 176
.OnAction = "SetPicklist"
End With
With ctButton2
.Style = msoButtonIconAndCaption
.Caption = "Set &Defaults"
.FaceId = 279
.OnAction = "SetDefaults"
End With
With ctButton3
.Style = msoButtonIconAndCaption
.Caption = "&Visibility Settings"
.FaceId = 2174
.OnAction = "VisibilitySettings"
End With
With cbToolbar
.Visible = True
.Protection = msoBarNoChangeVisible
End With
The 'OnAction' controls the macro that runs... If you wanted to expand that to run a macro on a specific workbook, use "whatever.xls!MacroName"

Related

Floating Message/Comment Box

So pretty much what I am trying to do is create a floating text box that would be on the right of the spreadsheet. When a user selects a row/cell it will then place a comment or message with details about that cell in there rather than a small little comment box.
I have tried to use the UserForm Box but its not really what I'm looking for.
Example:
User Selects Cell A4, I would like a message to read in a floating text when that cell is selected. Then if a user selects Cell B6 a different message appears in that box.
Does that makes sense?
Update:
The Following Code Shows a UserForm box when a certain cell is selected:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Definition As String
If Intersect(Target, Range("C6:D6")) Is Nothing Then Exit Sub
Select Case Target.Row
Case 22
Definition = "Text Here"
Case 23
Definition = "Text Here Again"
End Select
UserForm1.Label1.Caption = Definition
UserForm1.Show
End Sub
I don't want to use a UserForm box as its not stationary on the Worksheet itself. I want it so a Text Box that always appears on the right hand side of the worksheet to display a set message or context when the cell is selected. It will be different then what is stored in the actual cell.
Use a Data Validation message. This type of message "pops-up" whenever you click on a cell:
You can place a Label or TextBox control directly in the worksheet - make sure it's an ActiveX control, not a Forms control - and position it dynamically using the worksheet's SelectionChange event:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Me.lblText
.Visible = False
Select Case Target.Row
Case 22
.Caption = "Text Here Again"
Case 23
.Caption = "Text Here Again"
Case Else
.Visible = False
Exit Sub
End Select
' Place the label to the right of the target cell
.Left = Target.Left + Target.Width
' Or place the label in the far left of the window
'.Left; =; Application.ActiveWindow.VisibleRange.Width; -; .Width
.Top = Target.Top - 0.75 ' cell borders
.Visible = True
End With
End Sub
Some hints:
Make sure the 'Placement' property of a label is 2
(XlPlacement.xlMove), as other values give you Free-Floating or
Move-and-Size.
I strongly advise you to set the background colour of the control to
&H80000004&, the predefined Windows scheme colour for menu and form
backgrounds; likewise, the foreground to &H80000008&, the menu text
colour. This ensures visibility for users who have their own colour
settings, and explicitly supports users who specify an accessible or
assistive colour scheme to ameliorate a visual impairment.
My code relies on your sheet supporting event procedures in VBA, and
on ActiveX controls. It won't work in .xlsx sheets, and it may be
blocked (or accompanied by warning dialogues) if your operating
environment has a heavy-handed security policy.
Copy-and-paste might be affected by my use of the Worksheet
SelectionChange event.
Right-Click the control in design view for the 'Format Control' menu
and uncheck 'Print Control' - if the users print the sheet, they'll
want to see the cell contents, not the label.
Also: the label's in the way of selecting and editing the control
it's sitting over. Maybe you want it 4-5mm to the right, so the users
can get the mouse into that cell. Alternately, do this in the label's
Click() or MouseMove event:
Me.lblText.Visible = False
You might draw an obvious conclusion from all this: the native comment or data validation label is a better bet than using forms and ActiveX controls.

How to make a reusable button from a macro?

Having a working macro, I'd like to reuse it. Namely, to create a button on a toolbar (with a name + icon) that will launch a macro. Tried some tutorials (example: http://www.officetooltips.com/excel/tips/create_a_toolbar_button_or_menu_item_to_run_a_macro.html), but I'd also like to pack the creation code in some file, so that when clicked, the macro would be installed.
Is there an easy way to do it, and if yes, than how? (the best option would work for both Windows and Mac)
Update 4/20: great comment about the complexity of building an add-in. That being said, I'm sure the old timers here would say that something worth doing is worth doing right :). Here is a short walkthrough for creating an add-in:
(1) Save an xlsm or xlsb file with a name that's easy to increment for versions of your add-in.
(2) Add the following scripts into ThisWorkbook to ensure that you create a menu bar when the workbook is opened and when the workbook is activated:
Private Sub Workbook_Open()
Call CreateMenuBar
End Sub
Private Sub Workbook_Activate()
Call CreateMenuBar
End Sub
(3) Create a new module and add the following code to create, delete and update your menu bar:
Option Explicit
Sub CreateMenuBar()
Dim MenuObject As CommandBarPopup
Dim MenuItem As Object
Dim SubMenuItem As Object
'clear the old menu bar
Call DeleteMenuBar("&MyMenuBar")
'create the menu bar and drop down options
Set MenuObject = Application.CommandBars(1).Controls.Add(Type:=msoControlPopup, _
before:=10, Temporary:=True)
MenuObject.Caption = "&MyMenuBar"
MenuObject.OnAction = "UpdateMenuBar"
'first level menu option
Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup)
MenuItem.Caption = "&First Menu Stuff"
'link to first script
Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
SubMenuItem.Caption = "&First Script"
SubMenuItem.OnAction = "Script1"
'link to second script
Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
SubMenuItem.Caption = "&Second Script"
SubMenuItem.OnAction = "Script2"
'first level menu option
Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup)
MenuItem.Caption = "&Second Menu Stuff"
'link to third script
Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton)
SubMenuItem.Caption = "&Third Script"
SubMenuItem.OnAction = "Script3"
End Sub
Sub DeleteMenuBar(MenuName As String)
On Error Resume Next
Application.CommandBars(1).Controls(MenuName).Delete
On Error GoTo 0
End Sub
Sub UpdateMenuBar()
'do special checks, like verifying sheets, in this routine
End Sub
(4) Verify your scripts work and save the file.
(5) Save the file again as an xlam or xla file and distribute that to users. Boom!
--Original post below--
Here's what an add-in looks like on a Windows Excel instance:
And here's what an add-in looks like on a Mac Excel instance:
An add-in can be very handy if you develop many scripts for a fleet of users and want to ensure they're all using the same code.
I posted this on another question, but it wasn't what they were looking for. Would this work for you?
In the options for Excel, click on Customize Ribbon. Above the list of things you can add there should be a dropdown box where you can select Macros. The list should then be populated with macros to add to your ribbon!

Explain some of the parameters in this Word macro to me

I was using code at the bottom that I found on the Internet to add commands to the Word 2013 right-click menu. Now that I have used it I was hoping to understand it better and could someone explain some of the parameters to me. I want to understand it better and case I want to run it again to add more commands. The official Microsoft help reference only confuses me.
How does Before:=30 work? At first I thought it simply counted down from the top of the right-click menu, but when I did this my commands wound up in the wrong place. I think it must be counting commands that aren’t show all the time.
Are Tag:="Save" and .Tag = "Save" the same and what are they.
What is .Caption?
The above three parameters seem very similar.
Sub EditRightClickMenu()
'
'
'
Dim cb As CommandBar
Dim ctl As CommandBarButton
On Error GoTo bye
CustomizationContext = NormalTemplate
Set cb = CommandBars("Text")
Set ctl = cb.FindControl(Tag:="Save")
If ctl Is Nothing Then
Set ctl = cb.Controls.Add(Type:=msoControlButton, _
Before:=30, Temporary:=True)
With ctl
.Caption = "Save"
.Tag = "Save"
.FaceId = 3
.BeginGroup = True
.OnAction = "MySave"
End With
End If
bye:
End Sub
The Before:=30 is indeed the command on the menu before which you wish to insert the new control. If you remove the line On Error GoTo bye line from your code and run it on a normal word install you'll get an error trying to set the line:
Set ctl = cb.Controls.Add(Type:=msoControlButton, _
Before:=30, Temporary:=True)
This is because there aren't 30 controls on the default Text right click menu. Change it to 5 and it'll work fine.
Tag:="Save" and .Tag="Save" are slightly different. The Tag:= notation is used when specifying variable inputs to a method, in this case its the Tag input to the function of FindControl. If you press Shift+F2 on your keyboard when selecting FindControl in you editor it'll take you to the definition of the method and all the variables. The .Tag notation refers to an objects property, in this case the Tag is being set as "Save" so it can be found if the macro is run again.
Finally .Caption is merely the text displayed on the menu control once created.

excel macro calling another excel macro in vba

I have made a macro that calls another macro saved on my shared drive. I have taken the help from the suggestions given in the previous question I asked. It is working on the files that are on my local drive, but as soon as I open it from a file from the shared drive it stops working.
The macro name is mymacro.xla
These are the codes:
Dim i As AddIn
Set i = Application.AddIns.Add("M:\nit\USER\nitin kumar\NQK\macro\run1.2.xla", True)
i.Installed = True
I have created a button whose codes is given below:
Dim CoBa As CommandBar
Dim Ctlconst As CommandBarControl
On Error Resume Next
Set CoBa = Application.CommandBars.Add(Name:="Quote Daily Report", temporary:=True)
With CoBa
Set Ctlconst = .Controls.Add(Type:=msoControlButton)
With Ctlconst
.Caption = "Lexington Macro"
.Style = msoButtonIconAndCaptionBelow
.OnAction = "Accounts_Summary"
.FaceId = 483
.TooltipText = "NITIN"
End With
.Visible = True
.Position = msoBarBottom
End With
It's not working any suggestions would be of great help.
The least effort solution would be to copy the XLA locally before adding it.
The root cause could be one a number of things, locking, a read-only folder or security restrictions. It's better not to mess about with that and also that gets you around a bunch of potential problems such as:- what happens if you update the xla while someone is using it?

How to add icons to custom menu in excel - vba

I have created some custom menu referring "Custom Menu visible to one document in Excel". Now I want to add some icons to each menu item. Either that may be system icons or some others. Please provide some solution.
use the .FaceID property. If you want to know what id to use, then download a FaceID Browser addin for excel. Example:
Set MenuItem = NewMenu.Controls.Add _
(Type:=msoControlButton)
With MenuItem
.Caption = "Save XML Data"
.FaceId = 270
.OnAction = "AskExportXml"
.Enabled = True
End With