How do I enable Add-ins using VBA? - vba

I'm trying to enable one of Excel's default add-ins with VBA, and it looks like I'm typing a boolean instead of a method. All of the information I find tells me that this should enable the the Solver Add-in.
AddIns("Solver Add-in").Installed = True
But it doesn't. Instead, it returns "Compile Error: Invalid outside procedure."
How do I enable the Solver Add-in when a worksheet opens?
Edit:
I'm running this code because I have to put methods inside subroutines. The problem is that it returns "Compile Error: Can't find project or library."
Option Base 1
Sub TurnOnSolver()
AddIns("Solver Add-in").Installed = True
End Sub
TurnOnSolver
Function cubic_spline(input_column As Range, _
output_column As Range, _
x As Range)
' The function does stuff that requires Solver Add-in.
End Function

You can't call a method outside of a sub or function, it has to be within a code block.
I'd suggest putting the call in the workbook open event; double click the workbook object in the project explorer and paste this code in:
Private Sub Workbook_Open()
AddIns("Solver Add-in").Installed = True
End Sub
Alternatively, if you have a main sub put it in there. As a last resort you could put it in your cubic_spline function but presumably that will be called quite often so I'd advise against it.

Related

I am getting an error with this on load ribbon tab code

I'm using this code, but i'm receiving an error when excel tries to load it, works fine on excel 365 but on excel 2007 throws error:
<customUI onLoad="RibbonOnLoad"
xmlns="http://schemas.microsoft.com/office/2006/01/customui">
Public Rib As IRibbonUI
Sub RibbonOnLoad(ribbon As IRibbonUI)
Set Rib = ribbon
End Sub
Sub startHereConfigure()
Rib.ActivateTab "Configure"
End Sub
calling it here:
Private Sub Workbook_Open()
startHereConfigure '<<<<-getting object doesn't support this property or method
end sub
You can probably use Application.SendKeys to send Alt key combinations that can activate the Ribbon tab, but if the code is running in Excel 14.0 or above (2010+), then I'd keep the ActivateTab call, which is much more robust.
Since you're getting error 438, I take it that the member call is late-bound (resolved at run-time), otherwise the code wouldn't even compile, let alone run.
And when code is resolved at run-time, you can use conditional logic to make it, well, conditional - verify the Application.Version, and branch accordingly:
If Application.Version >= 14 Then
'Excel 2010+
Rib.ActivateTab "Configure"
''or explicitly late-bound:
'Dim ui As Object
'Set ui = Rib
'ui.ActivateTab
Else
Application.SendKeys "%C" ' assuming "Alt+C" activates the "Configure" tab; tweak accordingly.
End If

Excel VBA - How to use worksheet event in add-in module?

I am new to Excel Add-ins and I am not sure how to write mi programm.
I would like to put in an add-in a code so that, when the workbook that uses the add-in is opened, it creates a sheet named "mainSheet".
I can use the event handler in the Workbook, but is it possible to put the code in the module of the add-in and still be able to run it?
I found this on the "Automate Excel" web site. Hope this helps
The following code works opening a workbook. It automatically adds a new sheet and labels it with the name. It also checks to see that the sheet doesn’t already exist – to allow for the possibility of it being opened more than once a day.
This code makes use of the Workbook Open Event and must be placed in the workbook module under the “Open work Book” event. The function Sheet_Exists must be placed in a module and this checks whether or not the sheet exists:
Private Sub Workbook_Open()
Dim New_Sheet_Name As String
New_Sheet_Name = "mainSheet"
If Sheet_Exists(New_Sheet_Name) = False Then
With Workbook
Worksheets.Add().Name = New_Sheet_Name
End With
End If
End Sub
==
Function Sheet_Exists(WorkSheet_Name As String) As Boolean
Dim Work_sheet As Worksheet
Sheet_Exists = False
For Each Work_sheet In ThisWorkbook.Worksheets
If Work_sheet.Name = WorkSheet_Name Then
Sheet_Exists = True
End If
Next
End Function

Excel VBA to run after spreadsheet is refreshed

I've read lots of other posts (particularly this) and even tried to replicate a few and I don't understand what's wrong.
I'm trying to get a function to run after the spreadsheet has been refreshed. It seems that I have to mess around with query tables, though if there is some way that I can avoid that, please tell me. I've attached what I think are the relevant code snippets (though I could have missed some).
The error I get is runtime eror: '9' Subscript out of range
code in "this workbook"
Dim qtevent As qtClass
Private Sub Workbook_Open()
Set qtevent = New qtClass
Set qtevent.HookedTable = ThisWorkbook.Worksheets("REFRESH SHEET").ListObjects(1).QueryTable
MsgBox "Qt Events Run"
End Sub
Code in class qtClass
Option Explicit
Private WithEvents qt As Excel.QueryTable
Public Property Set HookedTable(q As Excel.QueryTable)
Set qt = q
End Property
Private Sub qt_AfterRefresh(ByVal Success As Boolean)
MsgBox "qt_AfterRefresh called sucessfully."
If Success = True Then
MsgBox "If called succesfully."
End If
End Sub

Running code from VBA to VBScript back to VBA

I'm trying to figure out a way to call a VBScript function using vba in Excel, and then pass a value back to excel-vba. See below
VBA within Excel
Sub RunTest()
Dim objString as String
'Begin Pseudocode
objString = Call VBScript Function Test()
'End Pseudocode
MsgBox objString `from VBS
End Sub
VBScript
Function Test
Test = "Hello World"
End Function
I know this may seem strange because I could just write the function in VBA, but we had an office patch pushed out and it completely killed the functionality of one of my macros for some reason. Strange thing is, I can run the exact same code within any other office program, just not excel. As a work around, I moved the function that crashes excel to word and I pull it using application.run, but I prefer to not have to do that, as opening a the word application to run my macro slows my process way down.
Any help is appreciated, thank You
Ok, I feel a litte dirty :)
This code has two key parts:
the vbs Uses GetObject and the full host workbook path to re-attach to the file containing the VBA that called the VBS
the VBS adds a value to a specific worksheet in the host VBA file to trigger the Worksheet_Change event to fire, running VBA with the string passed from the VBS.
Step 1: Regular Excel code module
Sub VBA_to_VBS_to_VBA()
Shell "wscript c:\temp\myvbs.vbs", vbNormalFocus
End Sub
Step 2: myvbs
Dim xlApp
Dim xlSht
On Error Resume Next
Set xlApp = GetObject("c:\temp\mybook.xlsx").Application
Set xlSht = xlApp.Sheets("vbs sheet")
On Error GoTo 0
If Not xlSht Is Nothing Then
xlSht.Range("A1").Value = "hello world"
Else
wscript.echo "sheet not found"
End If
Step 3: Sheet code for vbs sheet in your Excel File
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox [a1].Value, vbCritical, "VBS insertion"
End Sub
Try syntax like:
Sub Test()
Shell "cscript c:\TestFolder\sample.vbs", vbNormalFocus
End Sub

Methods in EXCEL Addin - XLL

How do I know which methods are available in my XLL module, in case i need to use / call any of them in my VBA code.
I can do this by calling the:
Application.Run()
method, in which I have to pass my macro-name as the parameter.
My question is about this macro-name: how do I know which macros are present in my XLL addin.
Any help is appreciated.
Cheers!!!!!!!!!!!
Tushar
You can use the Application.RegisteredFunctions method to give you a list of the functions in the XLLs that Excel has registered.
For example, the following code will list the XLL, the function name and the parameter types for the XLLs that are currently registered:
Public Sub ListRegisteredXLLFunctions()
Dim RegisteredFunctions As Variant
Dim i As Integer
RegisteredFunctions = Application.RegisteredFunctions
If IsNull(RegisteredFunctions) Then
Exit Sub
Else
Dim rng As Range
Set rng = Range("A1")
Set rng = rng.Resize(UBound(RegisteredFunctions, 1), UBound(RegisteredFunctions, 2))
rng.Value = RegisteredFunctions
End If
End Sub
Are you asking this from a code P.O.V? If you just want to check it out manually you can see that in the project explorer. Otherwise, I'd suggest just attempting to run the macro, but use an error handler in case the macro doesn't exist.
On Error GoTo badMacroCall
application.run(myMacro)
badMacroCall:
msgbox("That macro could not be run!")