我有一个组合框蛋白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
由于
答案 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
请注意.
之前的Range
和Cells