【问题标题】:compile error - object required编译错误 - 需要对象
【发布时间】:2014-10-22 06:46:25
【问题描述】:

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

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    

【问题讨论】:

  • 哪一行报错?

标签: vba object compilation required


【解决方案1】:

我可以看到您的代码存在一些问题: 1. 您正在使用“Set”来初始化 strCost、strFixedCost 和 strResourceCost,这不是必需的。随便写:

strFixedCost = 140
  1. 另外,您的第一个 IF 条件非常令人困惑。我想您正在检查 cResources 变量中是否存在值(您没有提到您在哪里获得 cResources 和 tQuantity 值)。在这种情况下,您可以使用If Len(cResources) > 0 Then

  2. 当您将字符串与整数组合时,您的第一个 msgbox 会给您一个类型不匹配的错误。

    MsgBox "价格为" & "$" + strCost, Buttons:=vbOKOnly, Title:="Cost"

相反,您可以使用以下代码将 strCost 转换为字符串:

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

【讨论】:

  • 感谢您的帮助!我现在还有一个错误。类型不匹配错误。但是它在此代码中。 strCost = strFixedCost + ((strResourceCost) * tQuantity) 其中 strCost、strFixedCost 和 strResourceCost 是整数,而 tQuantity 是来自我的用户窗体上的文本框的值。
  • 尝试使用 Cint(tQuantity) 函数并将其存储在整数中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
相关资源
最近更新 更多