我应该将用 VBA (Excel) 编写的大量代码转换为 VB6。但我真的不知道我必须照顾什么或从哪里开始。因此,如果能从 VB6 专家那里得到一些提示那就太好了。
我已经安装了 MS Visual Studio 并进行了一些尝试。但我不是 VB6 专家,并不真正知道我必须做什么。
最终目标是将当前放置在一个 Excel VBA 宏中的所有 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
编辑:是的,如果可以将 vba 代码转换为 .dll 这也可以。那么我就不必转换代码了。但我认为只能用 vb6 代码创建 .dll。
@Tom
好吧,我实际上正在和你一起学习这个,所以这里是,
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 构建解决方案
转到“项目”->“项目属性”并检查“注册 COm 互操作”
因此输出是 .DLL 和 .tlb ,Excel 文件应引用 .tlb 文件,
您必须通过 regasm /codebase c:\Excel\dllname.dll 注册 DLL
然后您可以从 Excel 访问该函数。
这是我的项目文件夹的链接,解压它,
您会发现一个 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/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)