2014-10-22 104 views
-2

我在我的代码中找不到任何错误,但是“对象需要”错误不断出现。 有人可以请帮忙。我一直在这,试图修复它半小时,我仍然无法找到问题。任何帮助,将不胜感激。 谢谢!编译错误 - 需要的对象

Private Sub cmdCost_Click() 

Dim strCost As Integer 
Dim strFixedCost As Integer 
Dim strResourceCost As Integer 
Dim wksResources As Worksheet 

Set wksResources = Application.Workbooks(1).Worksheets("Resources") 
Set strFixedCost = 140 


If cResources.Text = "" = False Then 
If Val(tQuantity.Text) > 0 Then 

wksResources.Select 
wksResources.Range("B2").Select 

Do Until ActiveCell.Value = cResources.Text 
ActiveCell.Offset(1, 0).Select 
Loop 

Set strResourceCost = ActiveCell.Offset(0, 3).Value 

Set strCost = strFixedCost + (Val(strResourceCost) * tQuantity) 

MsgBox " The price is" & " $" + strCost, Buttons:=vbOKOnly, Title:="Cost" 

Else 
MsgBox " You have not chosen a quantity.", Buttons:=vbOKOnly, Title:="Cost" 
End If 
Else 


MsgBox " You have not chosen a resource.", Buttons:=vbOKOnly, Title:="Cost" 
End If 


End Sub  
+0

哪一行会抛出错误? – 2014-10-22 07:15:15

回答

0

我可以看到一些问题与您的代码: 1.您正在使用“设置”来初始化strCost,不要求strFixedCost & strResourceCost。只要写:

strFixedCost = 140 
  • 而且,你的第一个IF条件是相当混乱。我想你正在检查cResources变量中是否存在值(你没有提到你获取cResources & tQuantity值的位置)。在这种情况下,你可以使用If Len(cResources) > 0 Then

  • 你的第一个msgbox会给你一个类型不匹配的错误,因为你将字符串与一个整数结合起来。

    MSGBOX “价格” & “$” + strCost,按钮:= vbOKOnly,标题:= “成本”

  • 相反,你可以使用下面的代码来strCost转换为字符串:

    MsgBox " The price is" + " $" + CStr(strCost), Buttons:=vbOKOnly, Title:="Cost" 
    
    +0

    感谢您的帮助!我现在还有一个错误。类型不匹配错误。但是它在这个代码中。 strCost = strFixedCost +((strResourceCost)* tQuantity) 其中strCost,strFixedCost和strResourceCost是整数,并且tQuantity是从我的用户窗体的文本框的值。 – 2014-10-22 08:12:49

    +0

    尝试使用Cint(tQuantity)函数并将其存储在一个整数中。 – rusk 2014-10-22 10:37:17