vba脚本有1004错误

时间:2016-03-04 21:31:35

标签: excel vba

我有一个与其他代码合并的代码。在他们自己的两个代码工作正常,但现在我遇到1004运行时错误,我无法弄清楚为什么它给第二行到最后一行1004错误" Set = Varrange ..."

Sub Simulate500k()

Dim i As Integer
Dim p As Integer
Dim k As Integer
Dim ROI As Integer
Dim Nummer As Integer
Dim NBC As Integer
Dim Spalte As Integer
Dim n As Integer
Dim m As Integer
Dim nblock As Integer




Nummer = Worksheets("Control").Cells(10, 2).Value
ROI = Worksheets("Control").Cells(9, 3).Value
NBC = Worksheets("Control").Cells(10, 5).Value
Spalte = Worksheets("Control").Cells(7, 5).Value
n = Worksheets("Control").Cells(11, 5).Value
nblock = Worksheets("Control").Cells(12, 5).Value


For k = nblock To 260

Worksheets("table").Cells(ROI * 17 + 1, 4 + 12 * NBC).Copy
Worksheets("Control").Cells(15, 2).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Worksheets("table").Cells(ROI * 17 + 1, 6 + 12 * NBC).Copy
Worksheets("Control").Cells(6, 2).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

For m = 1 To 14

 For i = 4 To 103

  Worksheets("500k").Cells(5, 2).Copy
  Worksheets("500k").Cells(2, i).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

  Set varRangeselect1 = Worksheets("500k").Range(Cells(6, 2), Cells(106, 2))
  Set varRangeSelect2 = Worksheets("500k").Range(Cells(6, i), Cells(106, i))
  varRangeselect1.Copy
  varRangeSelect2.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

1 个答案:

答案 0 :(得分:4)

您需要将父级分配给所有范围对象,包括Cells()内的Range()。最简单的是使用With块:

With Worksheets("500k")
    Set varRangeselect1 = .Range(.Cells(6, 2), .Cells(106, 2))
    Set varRangeSelect2 = .Range(.Cells(6, i), .Cells(106, i))
End with

因此,在With块中,任何以.开头的内容都会分配给With语句中声明的父母。