VBA what datatype is a dialogsheet? - vba

I can't find DialogSheets in the object browser in VBA. Dim dialog as ??? I like to see the autocomplete so I can know all the properties and methods of a particular type. VarType tells me it is just an object but is there a better or more specific way of finding out the type of an object? For example the OptionsButtons method... how am I supposed to know this thing even exists when it is not in the object browser?

You will not find them in the object browser because they are obsolete. They are just available for copatibility reasons.
You should use UserForms instead. In your vba editor click Insert, UserForm. Now you can easily create a userform with drag and drop.
Have a look here for more about dialog sheet: What exactly is a "dialog sheet" in Excel

Related

View names of controls in worksheet

So I feel a little foolish asking this question. I have spent plenty of time searching and only found a crude work-around. I have given this due diligence before posting.
For controls placed within a worksheet I cannot pull up the properties or even view the name to reference in code. The work-around I came across is if you right-click on the control (listbox in this case) and choose assign macro. It will suggest a macro name with the control name (e.g. ListBox13_change). In the code I refer to this listbox as Sheet2.ListBox13, but getting an error message for missing object.
With ActiveX controls or controls within a userform I can view the properties. I don't know why I am having trouble with this case.
Any guidance is much appreciated.
You can get the real name of the form control from the Immediate Window in the VBE by executing this line:
?Sheet2.Shapes(1).Name
Form controls are actually shapes in the object model.
The above assumes you have no other shapes on the worksheet.
Of course you can also see the name (and edit it) from the Name Box if the control is selected.
Once you have the name you can use it in code like so:
MsgBox Sheet2.Shapes("List Box 1").Top

How do I add a User Defined Function to Excel?

I have a simple stored procedure that returns a Description and a Name when you give it a ID. I need to enable this inline in multiple Excel Sheets. Something like =ItemLookup('12345') that would then return the aforementioned info.
I have not done a lot with Excel programming and am simply wondering what my options are for tackling this. Is this a VBA thing or should this be an external DLL that I COM register? Both felt like overkill but then I realized I had no idea if they were. I really wanted to use VSTO for this but it sounds like that is not possible for Cell level UDF's without having to modifiy each Workbook with some VBA.
The best way to add UDF functions to Excel is with Excel-DNA (which is a free, open-source library I develop), and any of the .NET languages - VB.NET, C# and F# are all fine.
To get started you make a new 'Class Library' project in Visual Studio (any edition), install the 'Excel-DNA' package from the NuGet package manager, and add your code:
Public Module MyDataAccessFunctions
<ExcelFunction(Description:="Gets the Item from the database")>
Public Function ItemLookup(code As String) As String
' Here you have to do some work to get the data
Return "Hello " & code
End Function
End Module
Pressing F5 builds and starts Excel, and you're done - try putting =ItemLookup("Paladin") into a cell.
The resulting add-in is a single .xll file, which you can copy and use on any machine that has .NET without any installation or admin permissions. It works with old Excel versions too.
The best place for support (including absolute beginners' questions) is the Excel-DNA Google group.
You can use Excel to create a VBA UDF pretty easily, just hit alt+f8, right click your project in the project hierarchy on the left of the screen, and click add module.
Here is a quick Hello World function you can just paste into the module, then click play (or alt+f8 from worksheets)
sub test()
msgbox "helloworld"
end sub
If it was me, I would probably just create a list of the file paths that need to be searched. Then create a VBA macro that opens them in excel, searches them for the key, and returns other information from the row the key was found on.
You can open files with the 'Application.Open' method, simply pass in
the file path as an argument. 'Application.Open' returns a workbook
object.
Each workbook will have several worksheets, you can access them
through the workbook's 'Worksheet' property
Getting each used cell in a workbook can be done via looping through the 'UsedRange'
property in each worksheet
Get the value of a cell for comparison from the cell's 'value' property
Cells also have a 'row' property so you can find other items on the same row
If you're used to VBA you could get this running in less than an hour. But since you're just starting out it'll probably take a 3+ hours since you'll have more research/debugging

Please let me know feasibility using Excel and VBA programming and suggest solution approach

We have following requirement to be done via excel and VBA programming. Please check and let me know if it feasible to do so and if not what way would be best to do it.
There would be excel spreadsheet say excel 2007/2010 with 2 tabs.
1st tab contains normal excel data
In 2nd tab user can enter a particular value and then click on a button in same tab. Is it possibly to have a button in excel tab and can it raise events like windows/web applications?
Now if button can be kept in excel and it can raise button click event then on such an event we need to create xml files. Is it possible to create xml files adhering to schema using excel &/or VBA programming
You need to activate the developer tab (from backstage go to Options and check the visibility of this tab) to get access to normal windows controls, like buttons and drop-down lists etc. You can easily place those in Excel sheets and write event handlers: the controls support the full range of events, including OnClick, OnMouseEnter etc...
You can write custom text files directly from VBA, but you can't validate those. The usual way to do so is just to concatenate small XML blocks into a bigger file, so XML output is more like a normal text output. But it works fine and as long as you do not need to perform something really complicated it is also ok.
EDIT: You can easily add references to custom COM objects (.dll or .ocx) and use them directly in your VBA code. One very helpful suggestion (see the comment below) is to use the MS XML library.
To add a reference just open the code editor (Alt + F11) and then go to Options -> References. Most of the references you need will be already in the list and you just have to check them, but you can also add your own custom libraries by clicking on Browse.

Calling VB.NET code from Excel

I open an Excel workbook from VB.Net and then want an event in the workbook (such as pressing a button) to activate code in VB.Net. How do I accomplish this type of callback?
Make the .Net assembly act as a COM object which is quite easy to use from VBA.
See this page for a comprehensive example:
http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/

Is there a way to get full IntelliSense for VBA in Access and Excel 2007?

In VB.NET, the IntelliSense pops up as soon as you start typing which gives you a pretty full list of things you can use at that moment. The IntelliSense in VBA however, doesn't kick on for me until a period is put after the part you're using. For example, I go into the VBA editor in Excel 2007 and start typing the word "Range" but the IntelliSense doesn't come up until I type "Range." after which it will give me a list of things I can use at that point.
Is there any way to make IntelliSense come up sooner in the VBA editor, so that I'm able to see a list of things that can be used like Davg, DCount, etc?
Just press Ctrl + Space at any time in the editor.
No, but you can type Excel first. Put a period after that, and you'll find out more than you ever wanted to know about Excel.
Same with Access. Type Access and a period, and you will find DCount there.
Unfortunately in Excel some objects are declared 'As Object' and Object doesn't have any methods so Intellisense won't be able to display any. The main culprit being Excel.ActiveSheet.
If you know what the type actually is then you can declare it explicitly and assign it to the value you want. EG:
Dim mySheet As Worksheet
Set mySheet = ActiveSheet 'This line would cause a type mismatch error if mySheet was declared as something other than a WorkSheet
mysheet.[All The Intellisense For a Worksheet Pops Up]
VBA is a different beast than .NET so I'm not sure how to bring up the IntelliSense quicker. I find I have the same problem you're having in 2003.
I would suggest checking out the Object Browser though. In 2003, it's View > Object Browser, or F2, in the VB Editor. I find it's a great way to explore the class libraries available. It will show you everything that you currently have referenced and once you reference more libraries, they will also show up in the Object Browser.