我的目标是尝试遍历Excel 2010工作表中的一组单元格,并在同一工作表中更改另一个单元格的值。这是我试图用VBA做到这一点的尝试。当我尝试更改moCost定义的单元格时,出现“Type MisMatch”错误。尝试使用VBA更改工作表中特定单元格的值失败
另外,我假设当我更改(使用工作表更改事件)我的工作表的单元格中的任何一个变量的值我使用设置它将更改moCostAfford si的值更正?只是想确认。
任何帮助/方向将不胜感激。
这里是我的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("I4:K42")) Is Nothing Then
On Error GoTo haveError
Application.EnableEvents = False
Dim moCost As Range, moCostAmount As Range, moCostManual As Range, moCostAfford As ValueChange
Dim cell As Range, affordabilityRowValues As Range
Set moCost = Range("A13")
Set moCostAmt = Range("B13")
Set moCostManual = Range("B14")
Set moCostAfford = Range("C13")
Set affordabilityRowValues = Intersect(Target, Me.Range("I4:K42"))
For Each cell In affordabilityRowValues.Cells
If cell.Offset(0, 1) = moCost And cell.Offset(0, 2) = moCostAmount And moCostManual = "0" Then
moCostAfford = cell.Offset(0, 3).Value
Else
moCostAfford = moCostAmount
End If
Next
Set affordabilityRowValues = Nothing
Set moCostAfford = Nothing
Set moCostManual = Nothing
Set moCostAmount = Nothing
Set moCost = Nothing
Application.EnableEvents = True
End If
Exit Sub
haveError:
MsgBox Err.Description
Application.EnableEvents = True
End Sub
先握住东西,在模块的顶部键入Option Explicit。这会警告你任何你没有声明或拼错的变量(提示:至少有一个)。之后,检查变量类型和您用于moCostAffordValue的属性。我从来没有使用ValueChange类型,但它显示值不是该类型的有效属性或期望的参数。 – Dave 2014-10-31 13:28:48
'moCost'是一个范围,但你没有在行中设置一个范围'Set moCost = Application.Cells.Value(“A13”)'看起来像'Set moCost = Range(“A13”)'或者' A13'有一个单元格地址,然后'Set moCost = Range(Range(“A13”)。Value)'类似于其他 – 2014-10-31 13:36:36
谢谢,伙计们!我会做出这些更正,并希望消除错误。 – Melinda 2014-10-31 13:40:48