I am trying to get some code working between Excel VBA and VB.Net. I have the following code in VB.Net in Visual Studio..
Public Class ThisAddIn
Private Sub ThisAddIn_Startup() Handles Me.Startup
End Sub
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
Public Function Multiplication()
Dim activeWorksheet As Excel.Worksheet = CType(Application.ActiveSheet, Excel.Worksheet)
Dim Range1 As Excel.Range = activeWorksheet.Range("A1")
Dim Range2 As Excel.Range = activeWorksheet.Range("A2")
Dim x As Integer = Range1.Value
Dim y As Integer = Range2.Value
Return (x * y)
End Function
End Class
I also have this code in VBA that I am trying to run
Sub Macro1()
Dim x
x = ThisAddIn.Multiplication
MsgBox (x)
End Sub
From Visual Studio I hit Start and I put this code into the Excel VBA for the new window that pops up.
I get the error '424 Object Required' on the VBA line "x = ThisAddIn.Multiplication". How can I call a VB.Net function from VBA?
Edit: I am using Excel 2013, VB.Net version 2013(VB12), and I guess I am targeting .Net version 4.5+
Edit: I tried adding the following code:
Imports System
Imports System.Collections.Generic
Imports System.Text
and
Private Sub Test()
Dim testClass As New ThisAddIn
MsgBox testClass.Multiplication()
End Sub
Based off this guide, but its still not working. I could not find the options to connect the project to Excel mentioned in Step 3 because it is an old guide. Maybe this is my problem?