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
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 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...
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
Using Excel 2011 (should be same as Excel 2010)
Code is under the "ThisWorkbook" module in Excel
Events are enabled
Macros are enabled
I can't seem to get either Workbook_Open() or Workbook_BeforeClose() to execute. I've read numerous posts on the subject but no solution. Here is some simple test code that should execute but doesn't. Any help would be greatly appreciated.
Private Sub Workbook_Open()
ActiveSheet.Range("BL4").Value = "Open is working"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next 'in case the menu item has already been deleted
ActiveSheet.Range("BL5").Value = "Close is working"
End Sub
First make sure you have put this in this in the right place and have macros enabled.
Then, try adding this line to the workbook_open method:
MsgBox "HELLO"
Do you see the msg box? You're choice of cell looks a bit strange
Also, I think you need to use a .xlsm file not .xlsx (Although not sure on that one)
FInally, if a plugin calls something like this line, it could cause your events not to fire..
Application.EnableEvents = False
So make sure you have tested it with no other sheets or addins open.