【发布时间】:2016-10-28 22:32:04
【问题描述】:
我正在尝试将选定的单元格复制到另一个工作表,但我总是收到错误消息:参数数量错误或属性分配无效
此代码检查“Cells(i, 20)”是否小于或大于“Cells (i, 4)”10%。如果不是,则删除该行,如果是,则应将选定的单元格复制到另一张从 48 行开始的工作表。
也许有人可以指出,我在这里做错了什么?这是我的代码的样子:
Sub CopyHighLow()
Sheets("ProductionHighLow").Select
i = 2
j = 48
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
produced = Cells(i, 20)
ordered = Cells(i, 4)
If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then
Cells(i, 22).Delete Shift:=xlUp
i = i - 1
Else
Range(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select
Selection.Copy Destination:=Sheets("Rytinis").Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5))
j = j + 1
End If
i = i + 1
Wend
End Sub
这里的更新是修改后的版本:
Sub CopyHighLow()
Sheets("ProductionHighLow").Select
i = 2
j = 48
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
produced = Cells(i, 20)
ordered = Cells(i, 4)
If Cells(i, 20) > Cells(i, 4) * 0.9 And Cells(i, 20) < Cells(i, 4) * 1.1 Then
Cells(i, 22).Delete Shift:=xlUp
i = i - 1
Else
Set RangeUnionCopy = Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20))
Set RangeUnionPaste = Union(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5))
RangeUnionCopy.Copy Destination:=Sheets("Rytinis").Range(RangeUnionPaste.Address)
j = j + 1
End If
i = i + 1
Wend
End Sub
【问题讨论】:
-
它在哪一行中断?出现错误时点击调试,在 vba 编辑器中查看暂停在哪一行
-
这两个调用有问题:
Range(Cells(), Cells(), Cells()...)Range方法最多只能接受两个参数(一个起始单元格和一个结束单元格) -
听起来你会想使用
Union而不是Range来解决这个问题 -
@Tom 调试时显示:Sub CopyHighLow() 不是范围线。
-
请注意:
.Select会减慢代码速度,并且在 99% 的情况下都可以避免。看到这个:stackoverflow.com/questions/10714251/… .