2014-07-11 113 views
1

我已經足夠長練成這樣VBA函數在Excel中返回值

IF(ISNUMBER(E6),IF(E6<standard_hour,0,IF(((E6-standard_hour)*24*60)>90,2,CEILING(((E6-standard_hour)*24*60)/30*0.5,0.5))),VLOOKUP(E6,Refer,2,FALSE)) 

公式,因爲我在電子表格中使用這個公式了很多,我決定做一個自定義的功能吧。這是函數

Function morning_check(start_hour) 
    Dim sheet As Worksheet 
    Set sheet = ActiveWorkbook.Sheets("Setup") 

    If WorksheetFunction.IsNumber(start_hour) Then 
     If start_hour < sheet.Range("E1").Value Then 
      morning_check = 0 
     Else 
      If ((start_hour - sheet.Range("E1").Value) * 24 * 60 > 90) Then 
       morning_check = 2 
      Else 
       morning_check = Application.WorksheetFunction.Ceiling(((start_hour - sheet.Range("E1")) * 24 * 60)/30 * 0.5, 0.5) 
      End If 
     End If 
    Else 
     morning_check = Application.WorksheetFunction.VLookup(start_hour, sheet.Range("Refer"), 2, False) 
    End If 

End Function 

此函數的輸入可以是字符串(例如:「TS」)或時間(例如:07:00)

使用字符串作爲輸入時,該功能正常工作,但當我使用時間只是拋出#Value!

+0

哪一行是錯誤的來源?如果您選擇調試,Excel應該突出顯示它。 –

+0

它的亮點第一行​​ – l1th1um

回答

1

你的錯誤是由以下行來:

Set standard_hour = TimeValue(sheet.Cells("E1")) 
Set user_hour = TimeValue(start_hour) 

在VBA Set用於創建對象,而你只是想設置一個變量。這就是爲什麼你會收到「Object Required」錯誤。

只要放下Set這個詞,你應該可以繼續進行調試。作爲一個調查問題(如果你的Excel副本的行爲與我一樣),Excel突出顯示了黃色的第一行(這沒有幫助),但它也自動選擇了文本standard_hour(它標識了問題)。

+0

嗨,我編輯我的代碼,現在它不會再次拋出錯誤,但它仍然不工作的非字符串(中頻的第一分支) – l1th1um

+0

只需忽略,現在一切工作正常,在最後一步我忘了格式化我的手機:D。無論如何,謝謝你的解釋。這是我第一次使用VBA – l1th1um