【发布时间】:2019-03-10 10:59:44
【问题描述】:
此代码旨在查看一系列单元格并将其值与参考单元格进行比较,然后根据 excel 的哪个部分与参考值匹配来更新某些单元格。我收到“无效的过程调用或参数”错误。当我使用 F8 浏览我的代码时,它似乎是在第二个 If 语句之后触发的。
Private Sub CommandButton1_Click()
Dim rng As Range, cell As Range
Set rng = Range("A171:AJ171")
For Each cell In rng
If cell.Value = ActiveSheet.Cells(4, 4).Value Then
If ActiveSheet.Cells((cell.Row + 1), cell.Column).Value = ActiveSheet.Cells("D3").Value Then
ActiveSheet.Cells("M45").Value = ActiveSheet.Cells((cell.Row + 3), cell.Column).Value
ActiveSheet.Cells("M46").Value = ActiveSheet.Cells((cell.Row + 4), cell.Column).Value
ActiveSheet.Cells("M47").Value = ActiveSheet.Cells((cell.Row + 5), cell.Column).Value
ActiveSheet.Cells("M48").Value = ActiveSheet.Cells((cell.Row + 6), cell.Column).Value
ActiveSheet.Cells("M49").Value = ActiveSheet.Cells((cell.Row + 7), cell.Column).Value
ActiveSheet.Cells("M26").Value = ActiveSheet.Cells((cell.Row + 8), cell.Column).Value
ActiveSheet.Cells("M27").Value = ActiveSheet.Cells((cell.Row + 9), cell.Column).Value
ActiveSheet.Cells("M28").Value = ActiveSheet.Cells((cell.Row + 10), cell.Column).Value
ActiveSheet.Cells("M57").Value = ActiveSheet.Cells((cell.Row + 11), cell.Column).Value
ActiveSheet.Cells("M59").Value = ActiveSheet.Cells((cell.Row + 12), cell.Column).Value
ActiveSheet.Cells("M60").Value = ActiveSheet.Cells((cell.Row + 13), cell.Column).Value
End If
End If
Next
End Sub
【问题讨论】:
-
您在
If ActiveSheet.Cells((cell.Row + 1)中有一个额外的(。你也有它在每一行的分配侧。最后,查看With Blocks。您会发现它有助于更快、更清晰、更易读的代码。
标签: excel vba if-statement