【发布时间】:2016-05-11 04:31:01
【问题描述】:
我遇到了一个让我很头疼的问题。归根结底,我认为问题很可能归结于我对 Windows 的 C 编程或如何在 C 和 VBA 之间正确工作缺乏了解。
我已经涉足 VBA 和 C 有一段时间了,我想我会将它们结合起来用于 Excel 项目,因为 VBA 执行速度不是很快。
有时在 Excel 中工作时,我需要从一长串值中查找一个值。对于一个值很容易(Ctrl+F),但有时我想要的值只能通过组合列表中的两个或多个值来找到。
出于这个原因,我编写了一个宏,它将值读入一个数组,然后循环遍历该数组,尝试将每个值与另一个值组合,看看它们是否组合成为我寻求的值。我现在已经将循环部分移动到一个用 C 编写的 dll 中,这当然加快了速度,但有一个问题:大多数时候——但并非总是如此——它无法找到真正结合到我的值中的值如果我寻求的值是十进制值,则寻求。
为了尝试找出问题所在,我让 dll 将所有测试的组合及其结果打印到一个文本文件中,我可以在其中看到匹配项,但由于某种原因,我的 if 语句没有触发它。
可能是什么问题?
这是我的 VBA 代码:
Private Declare Function FindVal Lib "mdvlib.dll" (ByRef dIn As Double, ByRef dOut As Double, ByVal iSizeIn As Long, ByVal sVal As Double, ByVal lvl As Long) As Long
Sub Match_Amounts(needle As Double, startcell As Range, level As Integer)
Dim haystack() As Variant
Dim i, j As Integer
Dim num As String
' dim variables going to the dll
Dim valArr() As Double
Dim valArr2() As Double
Dim arrSz, retval As Long
' read values from sheet into the array and find out its size
haystack() = Range(startcell, startcell.End(xlDown))
arrSz = UBound(haystack, 1)
're-dimension arrays that will be passed to the dll
ReDim valArr(1 To arrSz)
‘ using 100 here just to be on the safe side, will optimize later…
ReDim valArr2(1 To arrSz * 100)
' assign values
For i = 1 To arrSz
valArr(i) = haystack(i, 1)
Next
' change directory so that the macro finds the dll
ChDir Application.UserLibraryPath
' use the FindVal function in the dll
retval = FindVal(valArr(1), valArr2(1), arrSz, needle, level)
' present results
If retval > 0 Then
j = PresRes(valArr2, level, retval)
Else
num = Format(needle, "#,##0.00")
'Then show a message to the user
MsgBox "The value " & num & " could not be obtained by combining " & level & " values in the given range." _
& vbCr & vbLf & vbCr & vbLf _
& "This sometimes happens when searching for numbers with decimals. If this was the case, there could be values " _
& "that combine to make up the sought number.", vbInformation, "Match Amounts"
End If
Erase haystack
Erase valArr
Erase valArr2
End Sub
PresRes sub 只是向用户展示结果的一种方式,不应该是相关的。但是,如果您想看,请告诉我。
我在 dll 中与 VBA 交互的函数的 C 代码是:
int __stdcall FindVal(double* dIn, double* dOut, int iSizeIn, double sVal, int lvl)
{
if(lvl == 2) return FindValTwo(dIn, dOut, iSizeIn, sVal);
if(lvl == 3) return FindValThree(dIn, dOut, iSizeIn, sVal);
if(lvl == 4) return FindValFour(dIn, dOut, iSizeIn, sVal);
return -1;
}
从上面可以看出,我已经为三种不同的场景编写了 C 函数,用于查找两个、三个或四个加数,但这里我将只展示查找两个值的代码,因为该代码比其他人,我在所有这些功能中都有问题。
FindValTwo 函数的代码如下:
int FindValTwo(double* dIn, double* dOut, int iSizeIn, double sVal)
{
int i, j, k = 0;
FILE *dumpfile = NULL;
dumpfile = fopen("arraydump.txt", "a");
for(i = 0; i < iSizeIn; i++){
for(j = 0; j < iSizeIn; j++){
fprintf(dumpfile, "%f + %f = %f (%f) [%d][%d]\n", dIn[i], dIn[j], dIn[i] + dIn[j], sVal, i, j);
if(dIn[i] + dIn[j] == sVal && i != j){
fprintf(dumpfile, "\t^ found and added!\n");
if(ExistAlreadyTwo(dIn[i], dIn[j], dOut, k / 2) == 0){
dOut[k + 0] = dIn[i];
dOut[k + 1] = dIn[j];
k += 2;
}
}
}
}
fclose(dumpfile);
return k;
}
上面提到的文件写入行用于调试目的,不包括在内。 ExistAlreadyTwo 函数的代码是:
int ExistAlreadyTwo(double needle1, double needle2, double* haystack, int l)
{
// checks if the found values already exist in the return array
int i, existalready = 0;
for(i = 0; i < l; i++){
if((needle1 == haystack[i * 2] && needle2 == haystack[i * 2 + 1]) || (needle1 == haystack[i * 2 + 1] && needle2 == haystack[i * 2])){
existalready = 1;
break;
}
}
return existalready;
}
为了测试,我在 Excel 中做了一个简单的数组:
2.1
4.2
6.3
8.4
10.5
12.6
如果我搜索 21,我会得到一个命中报告,即 8.4 和 12.6 合并为 21。文本文件也验证了这一点:
8.400000 + 12.600000 = 21.000000 (21.000000) [3][5]
^ found and added!
再往下一点:
12.600000 + 8.400000 = 21.000000 (21.000000) [5][3]
^ found and added!
但是,当搜索十进制值时,例如18.9,我没有得到任何点击,即使文件表明这些值确实存在并且确实结合到了所寻求的值中。文本文件的输出:
8.400000 + 10.500000 = 18.900000 (18.900000) [3][4]
还有
10.500000 + 8.400000 = 18.900000 (18.900000) [4][3]
由于如果将两个十进制值组合成一个整数就会被拾取,起初我不认为问题出在将数组传输到 dll 而是在传输我正在寻找的值。
但是,我尝试在 C 中的 FindValTwo 函数中硬编码所寻找的值,使用以下行:
sVal = 18.9;
……但这也无济于事,没有找到。文本文件看起来与上面完全一样。
我已经尝试过 ByVal 和 ByRef(但只有 ByRef 用于数组),但我得到了相同的结果。我使用 32 位 Excel 2010(版本 14.0.7163.5000)。
【问题讨论】:
-
我已经发布了一些 cmets 作为答案。不过,它们可能帮不上什么忙 - 抱歉
-
可能是舍入问题。通常,始终使用 epsilon 比较浮点值,例如
if (f1 + f2 == f3)将变为if (abs(f1+f2 - f3)<eps),其中eps是一个与机器精度相关的非常小的值。 -
谢谢。哈维,我已经评论了你的回答,保罗,谢谢你的评论。它确实很有意义,我会研究一下!