I am creating a MIS (Management Information System) in Excel, and am required to make use of 'What-If Analysis'.
I'm in the process of adding various scenarios to the Scenario Manager, and to make it more user-friendly I am displaying the whole system in full-screen. This however means that the user won't be able to open the Scenario Manager.
I cannot simply assign each scenario to a button/drop-down list as the user needs to have the ability to edit each scenario. This therefore means that I'll need to program a VBA macro to launch the Scenario Manager.
This, however, has presented a problem: it doesn't seem possible to launch the Scenario Manager using VBA!
I've tried searching for it, but however all pages I've visited contain code for launching an individual scenario with the following:
ActiveSheet.Scenarios("[scenario name here]").Show
However there doesn't seem to be anything to launch the actual scenario manager:
I've also attempted to record a macro to do this, but the macro itself is empty:
Sub whatIfManager()
'
' whatIfManager Macro
'
' Keyboard Shortcut: Ctrl+Shift+W
'
End Sub
I tried ActiveSheet.Scenarios().Show on the off-chance that would work, but it doesn't (no method with that name).
Is it possible at all to launch the what-if scenario manager using VBA? If so, how?
The method you are looking for is in the Application object, specifically Application.Dialogs. Something like this will do it:
Application.Dialogs(xlDialogScenarioCells).Show
The enumeration value determines the dialog to show. The full list of dialogs is available here.
Related
Hi! I searched google already but found only 1 page mentioning how to do it in MS Access but not in MS Excel, here:
List an MS Access form's controls with their events.
I know how to list the controls on a userform. What I want to know is how to get to the list of events available to a control in the code editor (just clarifying).
Is there a way to programmatically list all available event procedures for a control on a userform like a command button, so that I can add that list of events to an array/collection/dictionary for other uses.
I can't find the control.properties as mentioned the referred link above since I am working in Excel, where this property is not exposed, I guess.
I wish I could go through a collection of properties/events like "for each oneEvent in oneControl.Events" and I know that it is not possible.
I think there must be an internal list/collection like that because we can go through Object Browser in VBA Editor or write event handler code in the VBA Editor.
Is there a way to access that list/collection (may be through VBIDE.VBProject)?
Many thanks in advance.
Hurrah!
I finally found the answer myself! Well, with a little help from #Tim Williams.
It took me 5 days to research the topic of TLI.
Even when my country, Myanmar, was under siege by an unlawful and awful military coup (on 01FEB2021), I couldn't stop thinking about this TLI issue I am facing, despite having to live through anguish, anger, pain, uncertainty and the feeling of being violated.
Now, I got it solved.
That said, I still don't really understand how the whole TLI thing works.
I think it would take me several years of reading on the subject matter to fully understand its declarations, mechanism, functions and methods.
Anyway, I will share what I found out through reading a lot of webpages and finally getting to the answer using a simple watch window to figure out how to get to the list of event procedure names that are available to a given userform control in VBA.
The following sub was taken from another stackoverflow page about listing the properties of userform object, I got stuck with it because I don't fully understand how the structure of the return data is formatted but that was overcome by looking at the structure using the Watch window in VBA Editor.
Requirement: Reference to TypeLib Information library at C:\Windows\SysWow64\TLBINF32.DLL
Sub listControlEventNames(ByVal o As Object)
Dim t As TLI.TLIApplication
Set t = New TLI.TLIApplication
Dim ti As TLI.TypeInfo
Set ti = t.ClassInfoFromObject(o)
Dim i As Integer
Dim mi As TLI.MemberInfo
For Each mi In ti.DefaultEventInterface.Members
i = i + 1
ActiveSheet.Cells(i, 1).value = mi.Name
Next
End Sub
This sub should be called with a userform control name like:
call listControlEventNames(UserForm1.ToggleButton1)
or
listControlEventNames Userform1.ToggleButton1
Finally, I can swear to God (but I'm a Free Thinker) that I can't really find how to list UserForm Control Events anywhere on the web, let alone a user manual on this library. The many many example code snippets, explanations and discussions that I found on TLI, were, for listing properties of UserForm Controls only.
Anyway, enjoy!
Like the title says, I have an Excel file with a macro running on load and I'd like to make it so the user can't use the computer while the macro is running.
It's a simple form for data input. However, most of the time users, just ignore it and let it run in the background while they work on other things.
Is it possible to make it so that while the Userform is being displayed, they can't do anything else on the computer forcing them to input the data?
I've tried everything from alerts to make them unable to close the form if they do not input data, but so far, nothing worked.
You can't do that. Your form is living in the EXCEL.EXE process space, and there is no way to tell Windows that EXCEL.EXE is all it's going to be doing until EXCEL.EXE says so.
This isn't a VBA limitation - you can't have a process that hijacks all message loops in every other processes on a machine, that's a recipe for disaster. I don't expect the OS to let you do that in any way, shape, or form.
Inline with the above, its best practice not to irritate your users. However, if its an application on a machine with a sole function, i.e a visitor log then you could make the form a topmost form.
see for instance https://www.mrexcel.com/forum/excel-questions/386643-userform-always-top.html for an example, and https://www.jkp-ads.com/articles/apideclarations.asp for declarations of the windows API functions
Another approach is to set a timer and alert your user with a sound...
I password protected my VBA Editor with:
Tools > VBAProject Properties > Protection
Clicked "Lock project for viewing"
Added a password.
What I need to have happen is that when this password prompt is accepted, It needs to run a Sub I've already written.
The answer I'm looking for and can't find in the three books on my desk is:
Is this action a Workbook Event? Or something else? And what syntax can make this happen.
Thanks!
Potential solution from MSDN:
https://msdn.microsoft.com/en-us/library/office/aa165442(v=office.10).aspx
This won't work for for what I'm trying to do. I wrote a script that asks for a key delivered weekly to authorized users via another program. What I wanted is a Sub to run which deletes all of my sensitive data if the user enters an incorrect password while attempting to get to the code.
Looks like this is not possible in Excel VBA... I'll keep learning and searching for a way to do this. If I do I will revisit this post.
(Questions with an answer of NO are still useful; they're just not the solution to the problem. Answers say, no, there is no built-in, you have to implement the dialog for yourself...)
In VBA, (ms-word, or ms-excel, but seems like a generic operation) is there any way to simply a provide a collection to a built in dialog in order to prompt the user to select a value from a list of values?
I can't believe there's not a built in method to do this, it seems like a such a generic operation that could be coded once and everybody would re-use it. I can certainly hand code it, but why bother if it's already in the vba libraries someplace.
I've searched for a solution, but it does appear that the standard answer is to hand code it.
My aproach would be to create a Form, add a ListBox, Ok, Cancel and the ShowModal property.
To use it first set the ListBox RowSource according to what you need:
https://msdn.microsoft.com/en-us/library/office/ff196460.aspx
Then make it visible, manage Ok/Cancel and then use the ItemsSelect property (multiselect is possible):
https://msdn.microsoft.com/en-us/library/office/ff823015.aspx
Yup, no such thing.
Hand-code it, and keep it as part of your VBA "toolbox" - make yourself an add-in that other VBA projects can reference, so you can reuse the code without having to rewrite it every time.
Then export the code modules from your host document, upload them to a GitHub repository, and share your solution with the world so the next person looking for it doesn't need to implement it from scratch again.
The VBA standard library is rather limited, and beyond MsgBox there isn't much available in terms of built-in UI. That's just how it is.
I was wondering if anyone could help me to disable the Advanced Filter option in Excel 2013.
I have used the following to disable all other data ribbon options:
.Protect UserInterfaceOnly:=True
But advanced filter is still able to be selected. With this they can unfilter the data.
The following code used to work in older versions of Excel but I cannot seem to get it to work for Excel 2013:
Application.CommandBars("Worksheet Menu Bar").Controls _
("&Data").Controls("&Filter").Controls("&Advanced Filter...").Enabled = False
If anyone could help me out with this or point me in the right direction then it would be appreciated.
Thanks guys.
Solution Suggested
The approach will be hiding the button from the user interface: the advantage on this solution is that the code is not in VBA (entirely) so, is not that easy for the user to modify it (or know what is going on backstage).
Answer Explanation/Next steps
It's really a deep answer with many steps involved in it so, I'll just post guidance and some useful links, that way you may research about it first.
1. Download Custom UI Editor for Microsoft Office: this helps to modify the user interface for the document.
2. Look for tutorials; start to code there with something basic like adding a tab and a button, (this is a good one)
3. Back to Excel, look for life cycle and custom events, this is a good place to start
4. You'll need to catch the event when the Ribbon is loaded; in that time, you'd need to disable the button, these links (1,2) are useful to show the steps and some example workbooks for it.
5. Locate the ID for the button and disable it (Hint: The idMSO = AdvancedFilterDialog, if you 've followed the previous steps, it will make sense here-).
Further information/Answer Disclaimer
Excel is not a software that can handle security deeply; if you are trying to restrict the user somehow in the original file, you should try other approaches -IG: a copy of a workbook for reference only, while the master one is saved in a sharepoint only modifiable by people authorized for it-.
While FAQ in S.O is strict about referencing to other sites without using a proper reference for it, this is a deep step solution that would take pages to explain even a random example; appealing to the guiding topic, this answer should be ok.