I need help with a project that I want to have something like a "Main Menu" Sheet. I added some shapes to act like buttons linking them with other excel files. What I needed the shapes to do is after clicking them, close this "Main Menu" Sheet as it is only needed to choose what type of sheet I need to use. So the idea is:
I have this "main-menu.xlsm" opened in the first place. Inside it's first sheet named "Main" I have 3 shapes linking to three other excel files "sheet1.xlsx", "sheet2.xlsx"and "sheet3.xlsx". The linking works just fine, but I end up with two Excel's opened, I need to make it so after I click the shape that opens "sheet1.xlsx" it also closes the "main-menu.xlsm".
What I've come across is adding this code to Sheet1(Main):
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveWorkbook.Saved = True
Workbooks("main-menu.xlsm").Close SaveChanges:=False
End Sub
But unfortunately it is not working with shapes. If instead of a shape I create a Text, link it to one of the files I need and click it, then the code works just fine. Anyone can help me out with this? Thank you for your time and patience reading this and helping if you can.
Try the next approach, please:
Remove the existing link, but copy its reference (firstly, only for one shape...);
Create the next Sub in a standard module
Sub MyShape1_Click()
Workbooks.Open ("Full name of the previously linked workbook")
ThisWorkbook.Close SaveChanges:=False
End Sub
You need to name each Sub in a way to be relevant when you need to assign it to the appropriate shape.
Right click on your shape(s), choose "Assign Macro...", select 'Macros in:' ThisWorkbook and look for the above Sub. Select it and click `OK'.
Enjoy what it will do...
Related
I built a series of macros, all of which have worked with the custom ribbon I built using the Custom UI Editor.
I added one more macro and updated my Ribbon with a new button. I test the macro first and it works when run from the developer tab or just running directly from VBA and the sub heading is as follows: "Sub GetDES()".
Once I make it so it works with the button on the ribbon by changing to:
Sub GetDES(control as IRibboncontrol)
I get the "Cannot run macro..." error
Sub GetDES(control as IRibboncontrol)
'PURPOSE: Get description page and insert into each confirm page
Dim Fpath As String
Fpath = ActiveSheet.Range("I6").Value
With Application.FileDialog(msoFileDialogFilePicker)
.ButtonName = "Add this DES!"
.AllowMultiSelect = False
.InitialFileName = Fpath
.Filters.Add "All Pictures", "*.*"
.Show
If .Show = -1 Then
Dim img As Object
Set img = ActiveSheet.Pictures.Insert(.SelectedItems(1))
img.Left = 75
img.Top = shOutput.Range("B40").Top
img.Width = 450
Else
MsgBox ("Cancelled.")
End If
End With
End Sub
The only thing I can think of is I still have something weird happening when I run it without the Control. When I run it the dialouge picker opens twice. I'm wondering if that is my problem with running it from the button on the ribbon.
When I step through the code, the line
set img=Activesheet.Picture.Insert(.SelectedItems(1))
is what causes the file dialogue picker to open again.
Ideas? Thank you
To make sure the callback is invoked by the Office you may try to assign a sample sub to a button, for example, with a simple MsgBox statement to make sure it works correctly.
Sub GetDES(control as IRibbonControl)
MsgBox "test"
End Sub
If that works correctly you can try putting your original code. But you may try to avoid shorthand properties like ActiveSheet or With operator. Hope it helps.
I believe I stumbled across my issue. I built a new module that I wrote this code in, I named the module with the same name as the sub. When Testing, I never went to run it from the macro list, I just ran it from the VBA window. When I looked at the Macro's List window, I saw that the new macro I wrote in this workbook was not displaying like the other macros. As you see in the photo, "Confirm Tool-Master_V5.xlsm'!GetDES.GetDES"
I removed that module and took the copy, listed as GetDES3 and renamed it
GetDES(control as IRibboncontrol)
and it runs from my ribbon. I'm not sure why it looked different or if there was something to do with the module name and the sub being named the same that caused it to be like that. so I tested it. I wrote a test sub and named the module test and it did the same thing.
So, Lesson is, don't name your sub and the module with the same name
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...
Here's my problem: I have two workbooks, one is mine, say Projet.xlsm, and the other is not, say Query.xlsm. Query.xlsm is shared between me and a lots of people in my company so I can't modify it.
My project.xlsm aims to plot some datas extracted with Query.xlsm. My job is to make it an automated process.
How Query.xlsm work: on the first sheet there is some options to select; then we have just to click a button on the first sheet. Finally all the datas are extracted and appears in second sheet.
I've built a macro in project.xlsm wich open Query.xlsm and select the options I want.
Now I'm tryi,g to "click the button" (sheet1 in Query.xlsm) from my macro in my project.xlsm. I've tried things like this: running excel macro from another workbook
But I think I have to mention the name of my sheet somewhere.
Thanks in advance
Assuming you have the workbook open (which it sounds like you do), this worked for me even on a Private CommandButton Sub.
Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = True
Workbooks("Query.xlsm").Sheets("Sheet1").CommandButton1 = vbClick
You may create a public procedure in Query.xlsm Workbook in first sheet.
cmdClick refers to the name of activex command button. Below is sample code.
Public Sub cmdClick()
CommandButton1_Click
End Sub
You can call this procedure from project.xlsm using below syntax.
Workbooks("testing.xlsm").Sheets("Sheet1").cmdClick
I'm very new to VBA and i searched and searched on Google, but can't find an example which deals with my problem.
I got a list of names which I want to put inside a selectable dropdownlist. When i click their name I want to run a different macro i made with their name on.
I tried a lot of things yesterday, but everytime it only succed me to assign 1 macro which was called no matter which name i pressed.
I think the solution is pretty simple, but i really got no clue how to do this the most simple way. So hopefully any of you got a link to a simple tutorial or can explain it to me in steps.
Thanks in advance
EDIT:
I got 2 names.
Birgitte = A:1
Thomas = A:2
I got a form comboxbox where both names are in.
When i press Birgitte i want a macro called BS_Opgave() to run and when i pres Thomas i want Macro TR_Opgave to run.
My problem is I'm not sure how to connect the combox selection to a Macro in the VBA editor. I'm acutyally confused about everything in the editor about comboxing.
Paste this code in a module. The Right Click on the Combobox and assign the macro DropDown1_Change to it :) And you are done.
Option Explicit
Sub DropDown1_Change()
With ThisWorkbook.Sheets("Sheet1").Shapes("Drop Down 1").ControlFormat
Select Case .List(.Value)
Case "Birgitte": BS_Opgave
Case "Thomas": TR_Opgave
End Select
End With
End Sub
Sub BS_Opgave()
MsgBox "You selected Birgitte"
End Sub
Sub TR_Opgave()
MsgBox "You selected Thomas"
End Sub
ASSUMPTIONS
I am assuming the following
The name of the combobox is "Drop Down 1"
The combobox is in "Sheet1"