2013-05-07 66 views
2

在我的Excel的VBA程序中;我有一個名爲「ParamCheck」的函數,它會得到四個「double」變量,檢查它們並以「string」的形式返回一條消息。當在VBA Excel中調用子函數時,「Object Required」

Function ParamCheck(airSpeed As Double, FCUvalue As Double, _ 
      altitude As Double, terrainElevation As Double) As String 

      If airSpeed < MIN_CONTROL_SPEED Then        
      'check if airspeed is less than 119 ft/min or not 
       ParamCheck = "Airspeed is less than minimum control speed" 

      ElseIf FCUvalue > FCU_VALUE_LIMIT Then       
      'check if FCU value if greater than 10 or not 
       ParamCheck = "FCU value is greater than limit" 

      ElseIf FCUvalue < 0 Then           
      'check if FCU vlaue is negative or not 
       ParamCheck = "FCU value is negative" 

      ElseIf altitude <= terrainElevation Then       
      'check if altitude is greater that terrain of elevation or not 
       ParamCheck = "Altitude is less than terrain elevation" 

      Else                
      'if all the parameters are valid print a "Valid" message 
       ParamCheck = PARAMS_OK 
      End If 
End Function 

現在我需要在我的子程序中調用這個函數。運行它給我這個錯誤「所需的對象」,並強調「checkParam」

回答

7

你不需要爲String類型的關鍵字Set,這就是爲什麼當這裏是代碼

Dim checkParam As String ' result of validity check of parameters 
Set checkParam = ParamCheck(speedAir, valueFCU, aboveSea, elevationTerrain) 

不像其它的編程語言,VBA認爲字符串作爲數據類型,而不是作爲一個對象

關鍵字Set用於指定對象(工作表,範圍等)的引用。

如果您嘗試爲數據類型分配引用,則會發生錯誤。

+0

非常感謝...我刪除了'設置',它只是工作 – user1143529 2013-05-07 22:50:53