Excel Macros run but don't show in Developer Tab - excel

I created a spreadsheet in the past which contained buttons that would run macros in the workbook. When I open the spreadsheet now and press the button, the macro will run perfectly. However, on the Developer tab, there are no macros listed and no code available. Is there a way to make the macros visible?

I do not understand. Is that your problem?
Option Explicit
Option Base 1
' This Macro is shown.
Public Sub X()
MsgBox ("X")
End Sub
' This Macro is hidden.
Public Sub Y(ByVal N As Long)
MsgBox ("Y")
End Sub

Related

WorkbookA macro to create hyperlink in WorkbookB to call a macro in WorkbookA without Worksheet_FollowHyperlink event

I have encountered a fun (I think) problem.
Let's say I have an .xlam Excel Add-In file which contains a macro
(any macro), even let it be Msgbox "Hello World!".
Let's say that I run this Add-In on new Excel Workbooks each time
(received from outside).
I would like the Add-In to insert a hyperlink into the Excel Workbook that would be opened.
I would also like that the inserted hyperlink would call the macro in the Add-In.
Now what I know/what I've tried:
1) If I was opening the same Excel Workbook each time I could just add the 'Follow Hyperlink' event: Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) into the Worksheet where I would like my hyperlink to be ran,
however,
Since I open a new file each time it's not going to have this piece of code in the Worksheet so the 'Follow Hyperlink' even won't work.
2) Now to solve the problem in point "1" I know that I could probably create another macro, which would insert the Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) code into the newly opened Excel Workbook i.e. a VBA code which writes a VBA code, however I don't think that's a very elegant way of solving this problem.
To sum up: I would like the (macro book) to insert a hyperlink into another workbook, and when that hyperlink would be clicked I would like it to run a macro from the 'macro book' WITHOUT using the Worksheet_FollowHyperlink event.
Any ideas? Thanks!
Try using the next simple way of accomplishing what you need, in fact:
Private Sub AddSheetEventCallProc()
'It needs a reference to 'Microsoft Visual Basic for Applications Extensibility x.x'
Dim wb As Workbook, wProj As VBIDE.VBProject, wCom As VBIDE.VBComponent
Dim wMod As VBIDE.CodeModule, Hrng As Range, LineI As Long
Set wb = Workbooks.Add 'any existing workbook can be used here
Set Hrng = wb.Worksheets(1).Range("A1") 'use here the cell you need
'Add the hyperlink:
Hrng.Hyperlinks.Add Anchor:=Hrng, _
Address:="", SubAddress:=Hrng.Address, TextToDisplay:="Call Macro"
With wb
Set wProj = .VBProject
Set wCom = wProj.VBComponents(Worksheets(1).codename)
Set wMod = wCom.CodeModule
With wMod
On Error Resume Next
LineI = .CreateEventProc("FollowHyperlink", "Worksheet"): LineI = LineI + 1
.InsertLines LineI, " If Target.Range.Address = """ & Hrng.Address & """ Then": LineI = LineI + 1
.InsertLines LineI, " Application.Run ""ADDINPRESA.xlam!testMsgbox""": LineI = LineI + 1
.InsertLines LineI, " End If"
On Error GoTo 0
End With
End With
End Sub
The second (more elaborate) way will be to make your add-in create a new Ribbon Tab:
Having the addin, it is good to create a new standard module naming it 'modRibbon', or similar. It is not mandatory, but it is good to separate the ribbon specific module from the one doing the add-in job. So, in this one where you must create the Subs able to be called by controls of the newly Tab to be created:
Option Explicit
Sub Test1(control As IRibbonControl)
appTest1 'use here your Sub name
End Sub
Sub Test2(control As IRibbonControl)
appTest2
End Sub
Sub Test3(control As IRibbonControl)
appTest3
End Sub
It is useful to copy this code in a Notepad window, in order to use it when the xml file will be created.
Then, create the module where the Subs called by the above ones will exist:
Sub appTest1()
MsgBox "It's coming...", , "Test1"
End Sub
Sub appTest2()
MsgBox "It's coming...", , "Test2"
End Sub
Sub appTest3()
MsgBox "It's coming...", , "Test3"
End Sub
Now, you need a, so named, OfficeRibbonEditor application. I use OfficeRibbonXEditor downloaded from here. Please, downoad 'OfficeRibbonXEditor-NETFramework.zip'. After downloading it, extract the file from archive and just use it. No installation need.
Now, run it and press Open button and browse for your add-in workbook and press OK. Select the open add-in and press 'Insert' menu. Select "Office 2010+ Custom UI Part" for Office 2010 or above. For 2007, select the other option. If you need the add-in to be compatible with both cases, both xml files will be inserted. I will show how to create a ribbon Tab only for first variant.
Double click 'customUI14.xml`, which has been created and copy the next code. Now, it is assorted with the above subs, but, in order to make it call your specific subs, use the Notepad window where you copied the Subs name:
Now, close the add-in from Excel and press Validate Button. If you made a mistake in the xml code, the line with the mistake will be highlighted. If everything OK, press 'Save' button and open your add-in. It will create a new Ribbon Tab (TestTab) having three buttons....
There are on the internet ways to find all imageMSO types. It is also possible to use your own icons. I did it some years before. I do not remember exactly how, but you need this option I can check my files archive.
For clarifying issues, a very good resource would be the Ron de Bruin site, which clarifies the way of doing such a Ribbon Tab, from more then ten years. You can find it here.
And the add-in does not 'import' the whole Ribbon. It only creates a new Tab adding it two the existing one and make it disappear when closed.
It is also good to know that when you modify the xml, all modifications done in the VBA add-in code will be lost and viceversa... That's why it is good to take care of closing the other one in the moment you modify something. If necessary, I can post a link to such an add-in done now, but, I would like to believe that it is easy to create one by yourself.
This is the way I create a new Ribbon Tab, but there are some other ways, too. When I will find some time, I will try doing it only in VBA. Practically, you have to read all subs you need to be called by your new Tab, then unzip the Excel file, create or modify the XML file in order to call your subs and rezip the file...

Sheet select from a Userform button causes data entered to go on previous sheet

I call a macro called SelectSheet1, from a button on a userform, to select Sheet1.
When data is entered afterwards it is put on the previous sheet.
This is happening on Excel 2013. I confirmed it is not a problem in Excel 2007.
Also it is not a problem if the macro is run directly from the developer tab, keyboard shortcut, quick access toolbar or ribbon customization.
The userform command button code:
Private Sub CommandButton1_Click()
Call SelectSheet1
Unload UserForm1
End Sub
The SelectSheet1 macro selects the sheet:
Sub SelectSheet1()
Sheets("Sheet1").Activate
End Sub
Link to xlsm file in dropbox
Link to youtube video if you want to see with your own eyes
It is a strange error and wondering if it a problem with Excel 2013, something changed in the way they do things and possibly there is a workaround.
Make sure there is only a single sheet Select'ed before you Activate a sheet:
Sub SelectSheet1()
Sheets("Sheet1").Select
Sheets("Sheet1").Activate
End Sub
Remember: "Although only a single sheet may be Active, many may be Selected."
I was able to figure out how to get it to work, but not sure why this solves the issue.
Im unloading the Userform before call macro and I tried using select instead of Activate, those did not help. But after I switched so that the UserForm loads with vbModeless then my problem went away, not sure why this fixed it.
For me the key to fixing this issue was vbModeless, but would love if somebody who understood more could explain why.
Private Sub CommandButton1_Click()
Unload UserForm1
Call SelectSheet1
End Sub
Sub ShowMainMenu()
UserForm1.Show vbModeless
End Sub
Sub SelectSheet1()
Sheets("Sheet1").Select
End Sub

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

Run excel VBA function when sheet is clicked

I was wondering if there was a way to run a VBA script when I open a sheet in the workbook.
For example, I have a workbook called "Inventory" and I want to run an "InitiateInventoryValues" Function when the "View Inventory" sheet is opened.
Can anybody please help me on this?
Double click the "Workbook" icon in VBE and use this event. It will trigger everytime you activate a different sheet by clicking its tab. If the tab is the one named "View Inventory", your code will run (once) when the sheet is activated:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "View Inventory" Then
'Do your code
End If
End Sub

Attach Existing UserForm Button to Excel Workbook Sheet

I have an Excel workbook that has a macro (the only macro in the workbook) attached to a button on a sheet.
In VB mode, I created a UserForm under Forms with a CommandButton1_Click Sub and when run from within VB (Run > Run Sub/UserForm or F5) it runs fine. I have it calling a Shell command that runs a BAT file that runs a Python script.
How do I run CommandButton1_Click from a button on a sheet? If I try to add a button to a sheet, it offers me the macro I have already associated with the other button.
Create one macro for example
Sub ShowForm
YourForm.show
End Sub
And associate this macro in button.
Why don't you move the main code into a new macro to modularise it. You can then call your macro via both UserForm and worksheet ActiveX (or Forms) buttons
'Normal Code Module
Sub TestCode()
MsgBox "Hi"
End Sub
'UserForm code
Private Sub CommandButton1_Click()
Call TestCode
End Sub
'ActiveX button code
Private Sub CommandButton1_Click()
Call TestCode
End Sub

Resources