【问题标题】:Range wrong number of arguments or invalid property assignment范围错误的参数数量或无效的属性分配
【发布时间】: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/… .

标签: excel vba


【解决方案1】:

问题说明
你的问题出在这一行

Range(Cells(j, 1), Cells(j, 2), Cells(j, 3), Cells(j, 4), Cells(j, 5))

Range 对象不能处理超过 2 个命名单元格(这种方式)。您可以直接在编译器中看到它。


更多信息请访问official documentation


方法解决方案:
在此之前我会使用 Union,如下所示:

Set RangeUnion = Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20))
RangeUnion.Copy Destination:=Sheets("Rytinis").Range(RangeUnion.Address)

这应该适用于您的目标。

【讨论】:

    【解决方案2】:

    使用 Union 更正代码:

    Sub CopyHighLow()
    
    Dim i, j, produced, ordered
    
    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
                Union(Cells(i, 1), Cells(i, 2), Cells(i, 3), Cells(i, 4), Cells(i, 20)).Select
                Selection.Copy Destination:=Sheets("Rytinis").Cells(j, 1)
                j = j + 1
            End If
    
    i = i + 1
    Wend
    End Sub
    

    【讨论】:

      【解决方案3】:

      你需要告诉它它是从哪张纸上复制的。

      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
                  ActiveSheet.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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多