【发布时间】:2014-01-04 05:38:46
【问题描述】:
我是一个偶尔的 VBA 程序员,为了好玩(不是我的工作)。
我在 MS Excel 2010 中有一系列 VBA 模块。无法弄清楚我做错了什么。这个例程奏效了,然后我改变了一些东西,它停止了工作。我做的一件事是将代码从一个函数拆分为两个模块中的两个函数。我认为在我将它分成两部分后它工作了一段时间,但现在我不记得这是不是真的,因为我已经尝试了很多东西让它再次工作。幸运的是,我将旧版本的所有代码保存在一个函数中,它仍然有效。它将一个大数组返回到电子表格。
基本上,我有一个调用函数的工作表。该函数调用另一个函数。结合使用 Debug - Toggle Breakpoints 和一些 MsgBox 调用,我发现第一个函数运行到它调用第二个函数的地步。然后第二个函数运行到“结束函数”命令。那时,工作表顶部的名称会闪烁几次……但什么也没有。调试时,程序似乎没有返回到第一个函数。应该在第一个函数中填充的数组用#Value 填充。
我阅读了几个可能损坏 VBA 的地方,因此关闭所有内容并重新启动可以修复它。它没有。然后我读到,如果您将所有内容复制/粘贴到新工作表中,并带有新模块(是的,大量复制/粘贴),则可以清理它。没有。
我还读到了一个问题,即当维度是函数的输入变量时,维度数组会出现问题。所以我初始化了用于设置数组维度的变量,即使它们是函数的输入变量。也许这是个问题?
代码真的很长,所以我只包含了对第二个函数的调用、第二个函数中的变量声明以及其他一些内容。也许我在传递变量时搞砸了一些语法?
第一个函数:
Option Explicit 'forces all variables to be explicitly declared
Function InputOutputDVL(InData As Variant)
'---------------------------------------------------------------------------------------------
Dim g, p, ng, np, ns, ID, count As Integer
Dim ngmax, npmax, nsmax, namax, nxmax, nymax As Integer
Dim row As Integer
Dim panelmax As Integer
Dim TLstyle As Integer
Dim Group(), Part(), Section(), Airfoil() As Variant
Dim ABP(), TV() As Double
ngmax = 20
npmax = 100
nsmax = 1000
namax = 10
ReDim Group(1 To ngmax, 1 To 4)
ReDim Part(1 To npmax, 1 To 6)
ReDim Section(1 To nsmax, 1 To 17)
ReDim Airfoil(0 To 100, 0 To 2 * namax + 1)
'missing code here
MsgBox ng & " " & np 'This msgbox works correctly and give the right value for np
ABP = Section2VL(nxmax, nymax, ns, panelmax, Group, Part, Section, Airfoil)
MsgBox "Completed Section2VL" 'The code never gets to this msgbox
InputOutputDVL = ABP 'I've tried setting this to = 1234 assuming there was a problem with
'ABP, but the cells on the spreadsheet are still #Value
End Function
第二个功能:
Option Explicit 'forces all variables to be explicitly declared
Function Section2VL(nxmax, nymax, ns, panelmax, Group, Part, Section, Airfoil)
Dim i, j, k, l, c1, c2 As Integer
Dim g, p, s, ng, np, chord, span, panel, ID, count As Integer
Dim NX, NY As Integer
Dim station, panelID, panelIDref As Integer
Dim pi, Xstyle, Ystyle As Double
Dim angle, dist As Double
Dim sx(), sy() As Double
Dim Scoord(), ABP() As Double
ns = 6
nxmax = 12
nymax = 12
panelmax = 300
ReDim sx(0 To nxmax), sy(0 To nymax)
ReDim Scoord(1 To ns, 0 To nxmax, 1 To 3), ABP(1 To panelmax, 1 To 32)
MsgBox ABP(panelmax, 5) 'This call works, and provides the proper value in the msgbox
'return ABP vector
Section2VL = ABP
'I've also tried just returning an integer thinking there was something wrong with the
'ABP array, like 987, but that doesn't work either
End Function 'This is where it stops when debugging. Doesn't return to first function
提前致谢。我已经度过了两个漫长的夜晚,无法弄清楚。
【问题讨论】:
标签: vba excel excel-2010 user-defined-functions