试图运行这个程序,但得到错误"编译错误,错误的参数数量或无效的属性赋值"

时间:2017-06-10 14:27:22

标签: excel vba excel-vba

Private Sub CommandButton1_Click()

LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To LastRow

        If Cells(i, 1) = "Wheat" Then
            Range(Cells(i, 2), Cells(i, 3), Cells(i, 4)).Select
            Selection.Copy


            Workbooks.Open Filename:="C:\commodities\allcommodities-new.xlsm"
            Worksheets("Sheet2").Select

            erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row


            ActiveSheet.Cells(erow, 51).Select
            ActiveSheet.Paste
            ActiveWorkbook.Save
            ActiveWorkbook.Close

        End If

    Next i

        For i = 2 To LastRow

            If Cells(i, 1) = "Feeder Cattle" Then
            Range(Cells(i, 2), Cells(i, 3), Cells(i, 4)).Select
            Selection.Copy


            Workbooks.Open Filename:="C:\commodities\allcommodities-new.xlsm"
            Worksheets("Sheet2").Select

            erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row


            ActiveSheet.Cells(erow, 3).Select
            ActiveSheet.Paste
            ActiveWorkbook.Save
            ActiveWorkbook.Close

        End If


    Next i

        For i = 2 To LastRow

            If Cells(i, 1) = "Corn" Then
            Range(Cells(i, 2), Cells(i, 3), Cells(i, 4)).Select
            Selection.Copy


            Workbooks.Open Filename:="C:\commodities\allcommodities-new.xlsm"
            Worksheets("Sheet2").Select

            erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

            ActiveSheet.Cells(erow, 67).Select
            ActiveSheet.Paste
            ActiveWorkbook.Save
            ActiveWorkbook.Close
        End If

    Next i
end sub

注意:代码在第一个"范围"命令带有"编译错误, 错误的参数数量,或无效的属性赋值"我可以在Range命令中使用2个单元格定义运行代码。

1 个答案:

答案 0 :(得分:2)

当你可以状态range("B1, C1, D1")时,你无法陈述range("B1", "C1", "D1")这就是你想要做的事情。

如果您确实需要第<3>,第3行和第4行 i ,那么只需使用第一个和第二个,如range("B1:D1")

Range(Cells(i, 2), Cells(i, 4)).Select

如果实际列是不连续的组,则使用Union。

dim rng as range
set rng = union(Cells(i, 2), Cells(i, 4), Cells(i, 6))
rng.select

请查看How to avoid using Select in Excel VBA macros

Option Explicit

Private Sub CommandButton1_Click()
    Dim i As Long, lastRow As Long, nextRow As Long
    Dim wbACN As Workbook

    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    Set wbACN = Workbooks.Open(Filename:="C:\commodities\allcommodities-new.xlsm")

    For i = 2 To lastRow
        Select Case LCase(Cells(i, 1).Value2)
            Case "wheat"
                Union(Cells(i, 2), Cells(i, 3), Cells(i, 4)).Copy _
                  Destination:=wbACN.Worksheets("Sheet2").Cells(Rows.Count, "AY").End(xlUp).Offset(1, 0)
            Case "feeder cattle"
                Union(Cells(i, 2), Cells(i, 3), Cells(i, 4)).Copy _
                  Destination:=wbACN.Worksheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
            Case "corn"
                Union(Cells(i, 2), Cells(i, 3), Cells(i, 4)).Copy _
                  Destination:=wbACN.Worksheets("Sheet2").Cells(Rows.Count, "BO").End(xlUp).Offset(1, 0)
            Case Else
                'do notbhing
        End Select
    Next i

    wbACN.Close savechanges:=True

End Sub