Where can I find a simple and useful list of VBA objects and methods for a beginner? - vbe

I tried pressing f2 to get the VBE thingo open and it just looks like jibberish to me.
I need something nice and simple which gives me a heirachy of what key words to use and where to put the dots so that I can do what I need to do and start learning this lanauge.
h = Worksheets("name_lists").range("g17").Offset(down, 0).Value
I don't know what worksheet is ? Is it a class with object range calling method offset ?
I need some sort of a resource to see what functionality is available but I can't make VBE do what I want.
Can someone please help me.
Thanks.

What you are asking for is at http://msdn.microsoft.com/en-us/library/office/ff194068(v=office.15).aspx. This should help you with documentation about individual objects and their properties/methods.
As for what functionality is available: (just about) everything you can do with the Office suite can be accessed through VBA. A good first step is to record a macro, and then try to follow the generated code. The caveat with that approach is that the recorder LOVES to use .Select and Selection., which is generally bad practice.
And, when you have a specific question you can't figure out, ask it here.

Related

Using Text Functions in VBA

I am using LEFT, UCASE, MID and a few other text functions.
eg If UCase(Mid(VBAFormField, 4, 3)) = "ABC" Then
For most users the code works fine. On one PC it gives an error and seems to need modification.
eg If UCase(Application.Worksheetfunction.Mid(VBAFormField, 4, 3)) = "ABC" Then
Things I tried:
Excel versions are identical
VBA Reference libraries are the same on both computers
Any ideas?
I have frequently experienced that VBA (2010) will highlight a missing library for an item for which the library is not missing because another library in fact is missing. It's usually just the first reference to any library in the code. So you should look for a missing library further down in the code, more likely, a not so standard object.
I have also frequently experienced (with any version of VBA up to 365) that errors aren't shown right away but are "remembered". I have explained this to myself by acknowledging that given a large project VBA will assemble the parts it needs as it needs them but once an error was discovered it won't be overlooked a second time and is flagged right away. This behavior could explain why only one of your PCs objects to the code. The others just didn't get around to it yet.
I would guess there must be an add-on or a language setting that is causing a conflict. However as Tim Williams pointed out, you're using the worksheet function in VBA when a VBA function exists that does this exact task you need to complete it.
While probably not necessary, you could go even further and scope the formulas to be vba specific just to ensure the proper library is being utilized.
MsgBox VBA.UCase(VBA.Mid("SomeText!", 4, 3))

Excel VBA - programmatically list available event procedures for controls on a userform

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!

Using the "extends" functionality in drools spreadsheets?

I have a question about using a certian drools functionality in drools decision spreadsheet, that would help a lot in reducing the files and making them more readable.
I can't add more than two links so please downlad this .zip file that includes:
Version1.PNG, Version1.drl, Version2.PNG, Version2.drl, Version3desired.drl
http://s000.tinyupload.com/?file_id=89653236807266194978
So here is the sample rule that we are using right now (something similar)Version1.PNG
And this when converted to a drl gives us the following 193 line long drl file. (Version1.drl)
It is ok and it works well, but after some research we found out we could use the "extends" functionality in drl.
And it works in a drl, what I would like to know is how would I use it in an Excel spreadsheet?
I designed the rule in this way: Version2.PNG
Clearly this is not how "extends" should be used in Excel, since this returns the following drl (Version2.drl)
You can see the extends is inside the "" of the rule name. Makes sense I guess, what I woudl like to know however is, how would I use it correctly? a seperate column? That didn't work, at least not the way I did it.
Does anyone know how It should be done?
The correct DRL that should be made after the Excel is converted is this: (Version3desired.drl)
Thank you for any help :)
Cheers!
There is no gain in using extends with spreadsheets in the way you describe it in your question.
I suggest that you stick to the spreadsheet layout shown in Version1.png. If data entry personnel complains about the dull repetition of 1/7/30 in oodles of rows, teach them how to join cells so they need to type each value only once.

Creating a string picker dialog from a collection in vba via built in dialog?

(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.

Disable Advanced Filter in Excel 2013

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.

Resources