将vba转换为vb6并创建.dll - 如何 - 提示,提示和风险

Tho*_*mel 4 excel vba excel-vba vb6-migration

我应该将大量用VBA(Excel)编写的代码转换为VB6.但我真的不知道我要照顾什么或从哪里开始.因此,从VB6专家那里得到一些提示会很棒.

我已经安装了MS Visual Studio并且玩了一下.但我不是VB6专家,也不知道我该做什么.

最终目标是将所有VBA代码(当前放在一个excel vba宏中)放入VB6项目中,然后创建一个.dll.这个.dll应该由excel引用,excel应该像现在一样运行:-)

例如,我需要做什么才能将此vba代码转换为VB6.

Public Function getParameterNumberOfMaterial() As Integer
10        On Error Resume Next
          Dim a As String
20        a = Sheets("Parameters").name

30        If IsNumeric(Application.Worksheets(a).range("C3").Value) Then
40            If Application.Worksheets(a).range("C3").Value > 0 Then

50                getParameterNumberOfMaterial = Application.Worksheets(a).range("C3").Value
60            Else
70                MsgBox "Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero"
80                MsgBox "Parameter Number of Material/Cost is set to the default value of 10"
90                getParameterNumberOfMaterial = 10
100           End If
110       Else
120           MsgBox "Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero"
130           MsgBox "Parameter Number of Material/Cost is set to the default value of 10"
140           getParameterNumberOfMaterial = 10
150       End If
160       On Error GoTo 0
End Function
Run Code Online (Sandbox Code Playgroud)

编辑:是的,如果可以将vba代码转换为.dll,这也没关系.然后我不必转换代码.但我认为只能用vb6代码创建.dll.

Viv*_*ard 5

@汤姆

好的,我实际上和你一起学习这个,所以这里,

VB.Net代码(我正在使用.net 2.0)


在Visual Studio 2005中打开一个新的类库项目然后删除已经写入的所有垃圾并粘贴代码

'First thing to do is add a reference the Excel Runtime

Imports Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices


Namespace ExcelExample

' the following is an Attribute spcifying that the class can be accesses in a unmanaged (non-.net) way

Imports Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices


 Public Class ExcelVB


    Public Function getParameterNumberOfMaterial() As Integer
        On Error Resume Next
        Dim a As String
        Dim appInst As New Microsoft.Office.Interop.Excel.Application
        a = appInst.Sheets("Parameters").name

        If IsNumeric(appInst.Worksheets(a).range("C3").Value) Then
            If appInst.Worksheets(a).range("C3").Value > 0 Then

                getParameterNumberOfMaterial = appInst.Worksheets(a).range("C3").Value
            Else
                MsgBox("Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero")
                MsgBox("Parameter Number of Material/Cost is set to the default value of 10")
                getParameterNumberOfMaterial = 10
            End If
        Else
            MsgBox("Please check cell C3 in the sheet 'Parameters'. It should include a numeric value which is greater than zero")
            MsgBox("Parameter Number of Material/Cost is set to the default value of 10")
            getParameterNumberOfMaterial = 10
        End If
        On Error GoTo 0
    End Function
End Class

End Namespace



按F6建立解决方案转到Project-> Project Proerties并检查注册COm互操作

所以输出是.DLL和.tlb,Excel文件应该引用.tlb文件,

你必须通过regasm/codebase c:\ Excel\dllname.dll注册DLL

然后,您可以从Excel访问该功能.

下面是我的项目文件夹unrar它的链接,你会找到一个excel工作簿,其中包含.tlb的.dll引用

http://cid-4af152a1af4d7db8.skydrive.live.com/self.aspx/Documents/Debug.rar

这是另一篇伟大的文章

http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/