簡體   English   中英

VBA UDF在測試中有效,但在excel中使用時不起作用

[英]VBA UDF works at test but not when used in excel

我對此感到絕望!

我已經建立了下面的功能。 cday指的是excel中帶有日期的單元格,例如01/01/2017

它匹配並找到數據集的行和列,並將其輸入到數組(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存在幾個問題:

  • lastday lastrow和totalcomm在任何地方都沒有定義
  • 您不能在UDF中使用sheet.activate或window.activate
  • 您應該使用Option Explicit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM