VBA UDF在testing中工作,但在Excel中使用时不起作用

我越来越绝望了!

我已经build立了下面的function。 cday是指excel中的一个单元格,如01/01/2017

它匹配并find一个数据集的行和列,并将其input到一个数组(dpricebase),这是我预先用数据填充的一个子

我写了一个小小的东西来唤起它,它起作用。 我尝试直接在Excel中使用它,而不是!

请帮忙!

谢谢!

Function findprice (cday, commno) cday = DateValue(cday) price1 = 0 If cday > lastday Then 'last day is a date xx/xx/xxxx at a cell in the excel price1 = 0 GoTo result End If Windows("m.xls").Activate Sheets("Daily").Activate 'find the matching row For x = 5 To lastrow If DateValue(Cells(x, 2)) = cday Then datarow = Cells(x, 2).Row - 5 GoTo Continue1 End If Next x MsgBox "No data for " & cday & " for " & commno Continue1: 'find the matching column For y = 3 To totalcomm If Cells(2, y) = commno Then datacol = Cells(2, y).Column - 3 GoTo Continue2 End If Next y MsgBox "No Daily data for No." & commno Continue2: price1 = dpricebase(datarow, datacol) GoTo result result: findprice = price1 End Function 

你的UDF有几个问题:

  • 昨天lastrow和totalcomm没有被定义在任何地方
  • 你不能在UDF中使用sheet.activate或window.activate
  • 你应该使用Option Explicit