Excel宏根据comboBox项目进行复制和粘贴

时间:2012-03-06 04:37:52

标签: excel excel-vba vba

我有一个组合框蛋白excel,基于其中选择的项目我想将数据从一张纸复制到另一张。下面是我运行它时代码的一部分我得到“运行时错误1004”我是在VBA中启动的

Private Sub ComboBox1_Change()

     Dim firstLimit As Integer
     Dim secondLimit As Integer
     firstLimit = 2
     secondLimit = 2

     Application.ScreenUpdating = False
     Worksheets("output").Range("A2:U2").Value =   Worksheets("Input").Range(Cells(firstLimit, "A"), Cells(secondLimit, "U")).Value
     Application.ScreenUpdating = True


     End Sub

由于

1 个答案:

答案 0 :(得分:1)

错误是由对Cells的无限制调用引起的。在您的代码中,这些将引用活动工作表。如果这不是“输入”,则会发生错误。

更改为

Sub ComboBox1_Change()
    Dim firstLimit As Integer
    Dim secondLimit As Integer
    firstLimit = 2
    secondLimit = 2

    Application.ScreenUpdating = False
    With Worksheets("Input")
        Worksheets("output").Range("A2:U2").Value = .Range(.Cells(firstLimit, "A"), .Cells(secondLimit, "U")).Value
    End With
    Application.ScreenUpdating = True


End Sub

请注意.之前的RangeCells