Vba 函数调用另一个函数,但得到;";

Vba 函数调用另一个函数,但得到;";,vba,excel,excel-2010,user-defined-functions,Vba,Excel,Excel 2010,User Defined Functions,我偶尔做VBA程序员,只是为了好玩(不是我的工作) 我在MS Excel 2010中有一系列VBA模块。不知道我做错了什么。这个程序奏效了,然后我改变了一些事情,它停止了工作。我做的一件事是将代码从一个函数拆分为两个模块中的两个函数。我想在我把它一分为二之后,它工作了一段时间,但现在我不记得这是不是真的,因为我已经尝试了很多方法使它再次工作。幸运的是,我在一个函数中保存了旧版本的所有代码,它仍然可以工作。它向电子表格返回一个大数组 基本上,我有一个调用函数的工作表。该函数调用另一个函数。将调试切

我偶尔做VBA程序员,只是为了好玩(不是我的工作)

我在MS Excel 2010中有一系列VBA模块。不知道我做错了什么。这个程序奏效了,然后我改变了一些事情,它停止了工作。我做的一件事是将代码从一个函数拆分为两个模块中的两个函数。我想在我把它一分为二之后,它工作了一段时间,但现在我不记得这是不是真的,因为我已经尝试了很多方法使它再次工作。幸运的是,我在一个函数中保存了旧版本的所有代码,它仍然可以工作。它向电子表格返回一个大数组

基本上,我有一个调用函数的工作表。该函数调用另一个函数。将调试切换断点与一些MsgBox调用结合使用,我发现第一个函数一直运行到调用第二个函数为止。然后第二个函数运行到“End function”命令。此时,工作表顶部的名称会闪烁几次……什么也没有。调试时,程序似乎不会返回到第一个函数。应该在第一个函数中填充的数组用#值填充

我读过一些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 as integer, nymax as integer... ) as Variant

Dim Scoord(), ABP() As Double 'This was already correctly declaring ABP as Double Array, but
                              'now I realize Scoord was incorrectly a Variant Array, but it
                              'wasn't actually causing a problem with my code.
                              'I fixed this in my own code, but left here as example of what
                              'not to do!

ReDim ABP(1 To panelmax, 1 To 32)

'Code removed here

'return ABP vector
Section2VL = ABP 

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

提前谢谢。我熬过了两个漫长的夜晚,弄不明白。你所说的是
InputOutputDVL.ABP
Section2VL.ABP
Section2VL
本身之间不可比拟的类型

尝试将所有THSE
()声明为Double

InputOutputDVL

Dim ABP() As Double
第2VL节中

Dim ABP() As Double


关于类型声明的说明

当您声明这样的变量时

Dim i, j, k, l, c1, c2 As Integer
除最后一个以外的所有变量实际上都声明为
变量

您需要显式地指定每个变量类型


另外,除非有特殊原因,否则不要使用
Integer
。改用
Long

谢谢克里斯!你确实把我引向了正确的方向。我还使用了这两个网站:

http://www.cpearson.com/excel/passingandreturningarrays.htm

http://www.cpearson.com/excel/vbaarrays.htm
不完整/不兼容的变量声明是我问题的根源

首先,我忘记了VBA要求在每个变量后面加上“as Integer”或“as Double”,而不仅仅是在行的末尾。很多变量都是变量,而不是整数、双精度等。再次感谢Chris

其次,更具体地说,我只需要做上面提到的Chris的一个更改,那就是在第一个调用函数中将ABP()正确地声明为Double。ABP()已经在第二个函数中正确调用了Double,我没有将函数Section2VL声明为Double()。使用原始代码:

Dim ABP(), TV() as Double
这表明ABP是一个变体数组。后来对ABP=Section2VL()的调用试图将变量填充到变量数组中,这就是问题所在。我仍然很失望,编译器没有说它有不兼容的数据类型或其他错误。这也解释了问题的来源。该代码以前作为两个模块中的两个函数工作。在进行其他一些更改时,我注意到我没有将ABP声明为数组,所以我添加了“()”。我做了其他的改变,它停止了工作。我把重点放在了其他的更改上,而不是添加()标记的看似微不足道的“更正”。真正发生的是,在原始代码中,错误的组合导致了正确工作的代码!想象一下

因此,代码使用ABP作为变量,或使用ABP()作为双数组,但不使用ABP()作为变量数组

当然,这里的正确答案是Chris建议的,也就是正确明确地声明所有变量。一个有趣的注意事项是,变量(不是变量数组)本质上可以“接受”被分配任何传入值,包括双数组

在我的最后一段代码中,我将这两个函数都保留为变量,但现在我特别将它们声明为变量,以澄清这一点。在第二个函数中,ABP被正确地声明为一个动态双数组,然后被赋予适当的维度来分配内存空间。在第一个函数中,ABP可以是一个变量,也可以是一个双数组,两者都可以工作,但由于我一直希望它是一个双数组,所以我将其指定为双数组

Option Explicit 'forces all variables to be explicitly declared

Function InputOutputDVL(InData As Variant) as Variant
'---------------------------------------------------------------------------------------------

Dim ABP() as Double, TV() As Double 'This ABP was a variant array - which was incompatible

'code removed here

ABP = Section2VL(nxmax, nymax, ns, panelmax, Group, Part, Section, Airfoil)

InputOutputDVL = ABP 

End Function
第二个是函数:

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 as integer, nymax as integer... ) as Variant

Dim Scoord(), ABP() As Double 'This was already correctly declaring ABP as Double Array, but
                              'now I realize Scoord was incorrectly a Variant Array, but it
                              'wasn't actually causing a problem with my code.
                              'I fixed this in my own code, but left here as example of what
                              'not to do!

ReDim ABP(1 To panelmax, 1 To 32)

'Code removed here

'return ABP vector
Section2VL = ABP 

End Function