【发布时间】:2014-12-30 16:32:15
【问题描述】:
我是 VBA 的新手,使用的是 excel 2013。我整天都在为这个错误绞尽脑汁!标有星号的行出现
“应用程序定义或对象定义错误”
我的功能应该做两件事...
1) 给当前单元格一个值
2)改变另一个单元格的填充颜色
#1 没有问题,但#2 不断出错
这是我目前正在使用的代码...
我从 excel 的单元格中调用它,给它公式:=ScoreIt()
Function ScoreIt()
Dim TotalVal As Integer, LRVal As Integer, LYVal As Integer, LGVal As Integer
TotalVal = 0
LRVal = 0
LYVal = 1
LGVal = 2
Dim CurrentRow As String, BedCell As String, Beds As Integer
CurrentRow = ActiveCell.Row
BedCell = Range("K" & CurrentRow).Address(False, False)
Beds = Range(BedCell).Value
If (Beds < 2) Or (Beds > 5) Then
TotalVal = TotalVal + LRVal
** Range(BedCell).Interior.ColorIndex = 38 **
ElseIf (Beds = 2) Or (Beds = 5) Then
TotalVal = TotalVal + LYVal
** Range(BedCell).Interior.ColorIndex = 36 **
ElseIf (Beds = 3) Or (Beds = 4) Then
TotalVal = TotalVal + LGVal
** Range(BedCell).Interior.ColorIndex = 35 **
End If
ScoreIt = TotalVal
End Function
谢谢
【问题讨论】:
-
为什么不将
BedCell设为Range对象,然后调用set BedCell = Range("K" & CurrentRow),然后使用BedCell.Interior.ColorIndex = 38 -
我已经尝试过了,它也不起作用,BedCell 返回单元格中的值而不是单元格的地址/位置。然后我尝试将 .Address(False, False) 添加到它的末尾,但这只是以类型不匹配错误结束。