I think it would be simpler if I try to show you an example.
I want to multiply any number in cells(1,1) with 33.
In a module, write the following code:
Sub test_addIn()
Dim x, y As Integer
x = Cells(1, 1).Value
y = x * 33
Cells(1, 1).Value = y
End Sub
To be able to start execution, you'll have to make a menu. Add this to the module:
Sub AddNewMenu()
Dim HelpMenu As CommandBarControl
Dim MenuItem As CommandBarControl
Dim SubMenuItem As CommandBarButton
Dim NewMenu As CommandBarPopup
Set HelpMenu = CommandBars(1).FindControl(ID:=30010)
If HelpMenu Is Nothing Then
Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlPopup, temporary:=True)
Else
Set NewMenu = CommandBars(1).Controls.Add(Type:=msoControlPopup, before:=HelpMenu.Index, temporary:=True)
End If
NewMenu.Caption = "T&est"
Set MenuItem = NewMenu.Controls.Add(Type:=msoControlButton)
With MenuItem
.Caption = "XLA-test"
.FaceId = 162
.OnAction = "test_addIn"
End With
End Sub
As you only want the user to see your menuitem when using the xla, add also this:
Sub DeleteNewMenu()
On Error Resume Next
CommandBars(1).Controls("Test").Delete
End Sub
In ThisWorkbook, add the following code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
DeleteNewMenu
End Sub
Private Sub Workbook_Open()
AddNewMenu
End Sub
Now go back to your empty excel workbook, click X to close the workbook and say Yes to save it first. Choose to save it as an .xla file and give it a proper name. Maybe Office will change to the AddIn directory by itself before saving it. Otherwise you can do it yourself.
Save it and close Excel.
Open Excel and open a new workbook. You won't see the menuitem Test yet. Go to the menu Functions. In the bottom last section there should be an item where you can activate addins. Not sure about the english name but if you have saved the .xla in the AddIn library as described before, you should be able to find the name in the list. Mark the item. Now the menuitem Test should appear in your menu.
Write 123 in cells(1,1) and open the menu Test and click XLA-test. Now the value should be multiplied with 33.
In your case, it might be worth knowing that you can activate the .xla from any position just by opening it in Excel. When you do that, the only thing you'd notice is the new menuitem Test, no workbook appear. However, your macrocode is available from the menu.
To protect your code, open VBEditor, rightclick the .xla projectname and choose Project Properties. Under protection you can write a password which will be necessary to see the code.
This was a lot of words. Hope you can make it work bu if not let me know.
This address gives a more in depth description of creating AddIns. Also have a look at the 'Using Add-In Functions in Your VBA Code'.
http://www.fontstuff.com/vba/vbatut03.htm