我有一些代码正在从VB.NET迁移到在.NET世界中运行良好的VBA。到目前为止,除一个例外,我已经成功地将几乎所有代码移入了VBA世界。这里有很多问题代码和所有变量声明。
Dim vault As IEdmVault14
Dim eFile As IEdmFile9
Dim eFolder As IEdmFolder7
Dim pos As IEdmPos5
Dim Pathlist As EdmStrLst5
Dim parentFolder As IEdmFolder5
Dim vaultName As String
Dim filePath As String
Dim AssyName As String
Dim LoggedIn As Boolean
Set EdmVault5 = New EdmVault5
Set vault = New EdmVault5Dim fso As New FileSystemObject
Dim sw As TextStream
Set sw = fso.CreateTextFile("c:\temp\" & AssyName & ".txt")
'-----------------------------GET COLUMN HEADERS
Dim columns() As EdmBomColumn
BOM.GetColumns columns
Dim header As String
header = "LEVEL" & vbTab
Dim column As EdmBomColumn
For i = 0 To UBound(columns)
header = header & columns(i).mbsCaption & vbTab
Next
sw.writeline (header)
'-----------------------------Bom.READ EACH BOM ROW
Dim rows As Object
Dim row As IEdmBomCell
BOM.GetRows (rows)
For i = 0 To UBound(rows)
If IsNothing(row) Then Exit For
Dim rowString As String
Set rowString = row.GetTreeLevel.ToString & vbTab
Dim varVal As String
varVal = ""
For i = 0 To UBound(columns)
row.GetVar(column.mlVariableID, column.meType, varVal, Nothing, Nothing, Nothing)
If IsNothing(varVal) Then varVal = ""
rowString = rowString & varVal & vbTab
Next
'-----------------------------WRITE THE ROW TO THE FILE
sw.writeline (rowString)
Next
sw.Close
`
在BOM.GetRows(行)处发生数组错误。我被困在可能是什么问题上。在VB.NET中不会出现此错误代码,但是.NET会警告变量“行”在被赋值之前已通过引用传递。空引用异常可能在运行时导致。我完全不清楚如何将其转换为VBA。
如果有人可以对此有所了解,我相信会有所帮助。
答案 0 :(得分:0)
如果您有需要数组的方法签名(或函数签名或任何其他方法),则必须Dim
作为数组传入的变量。
Public Sub test()
Dim x As Variant
Debug.Assert Not IsArray(x)
x = Array(1, 2)
Debug.Assert IsArray(x)
GetStuff x 'this fails
Stop
End Sub
Public Function GetStuff(a() As Variant) As Double
GetStuff = 1
End Function
即使Variant可以容纳数组,也不能仅通过声明就通过IsArray
测试。如果我为它分配一个数组,它将传递IsArray
,但我仍然不能将其用作需要数组的函数的参数。 x
是Variant数组,而a()
是Variants数组。因此,上面的代码仍然无法编译。
从您的评论看来,您已经对它进行了排序,但我想我会在此补充一些信息,以供后代参考。