对于循环范围不起作用:参数数量错误或属性分配无效

时间:2015-09-18 15:42:12

标签: loops for-loop range

我有以下代码扫描原始数据列表和每行,如果date和var(我定义的变体)是正确的,则复制行并将其放入正确的目标。这行复制行已注释掉。该宏工作但不是复制我现在尝试的整行(在注释掉的.copy行上面的2行)而只选择行中的特定单元格(iRow中的单元格和M,N,O列, Q,R,U,V,AB)。我收到错误:参数数量错误或属性分配无效。我知道这与我输入的2个新代码行有关,因为以前代码运行正常。感谢任何帮助,谢谢!

Sub currentMonthDetail()

Dim csCount As Range
Dim b As Variant
Dim shrow As Long
Dim iRow As Long, iRowL As Long, var As Variant, iDate As Variant
Dim bln As Boolean
Dim s As String
Dim eRow As Integer
Dim i As Integer

'Import monthly data from GL008 tab to detail-----------------------------------------------------------------

Set csCount = Worksheets("Input").Range("csCount")

Sheets("GL008").Activate

iRowL = Cells(rows.count, 1).End(xlUp).Row

For iRow = 2 To iRowL
    Sheets("GL008").Activate
    If Not IsEmpty(Cells(iRow, 35)) Then
        bln = False
        var = Application.Match(Cells(iRow, 35).Value, Worksheets("Input").Columns(3), 0)
        iDate = Sheets("GL008").Cells(iRow, 34)
        If Not CVErr(var) = CVErr(xlErrNA) And iDate = Worksheets("Input").Range("E3") Then
        Sheets("GL008").Activate
        Range("M" & iRow, "N" & iRow, "O" & iRow, "Q" & iRow, "R" & iRow, "U" & iRow, "V" & iRow, "AB" & iRow).copy
        'Sheets("GL008").rows(iRow).copy
        s = Sheets("GL008").Cells(iRow, 35)
        Sheets(s).Activate
        eRow = Sheets(s).Cells(rows.count, 1).End(xlUp).Offset(1, 0).Row
        ActiveSheet.Paste Destination:=Sheets(s).rows(eRow)
        End If
    End If


Next iRow

1 个答案:

答案 0 :(得分:0)

您将大量单元格传递给Range。当你尝试这样做时,它会寻找一个左上角的起始单元格,一个右下角的单元格,并且会从一个单元格到另一个单元格进行矩形选择。

因为您尝试为其提供8个单独的参数,所以它无法处理它。你需要给它一个参数,其中包含单元格列表;将它们全部放入一个字符串中,然后将该字符串作为一个参数提供给范围。

即。将逗号放入文本。

Range("M" & iRow & ",N" & iRow & ",O" & iRow & ",Q" & iRow & ",R" & iRow & ",U" & iRow & ",V" & iRow & ",AB" & iRow).copy