VBA运行时错误9

时间:2017-01-05 17:21:11

标签: excel-vba vba excel

首先,我想说的是,如果我的问题非常基础,我是VBA的新手,也不知道。

对于下面的代码,我以粗体显示该行的运行时错误9。我原本以为是由于我的工作表引用,我尝试更改为Sheet1,Sheet2等,但这并没有解决错误。

对我所做错的任何输入都将不胜感激。

Sub GMP_CALC()

Dim wsin As Worksheet
Dim wsout As Worksheet
Dim wscen As Worksheet

Dim i As Long
Dim k As Long
Dim j As Long
Dim rnum As Integer

Dim record(15) As Variant
Dim val As Variant
Dim ref As Variant

Debug.Print ActiveWorkbook.Name

Set wsin = Worksheets("DATA")
Set wsout = Worksheets("OUTPUT")
Set wscen = Worksheets("CALC")

With wsin
  rnum = .Range("A" & .Rows.Count).End(xlUp).Row
End With

rnum = rnum - 1

For i = 1 To rnum
  ref = wsin.Cells(i + 1, 6).Value
  For k = 1 To 25
    **record(k) = wsin.Cells(i + 1, k + 2).Value** '<-----This line errors
  Next k
  For j = 1 To 25  
    wscen.Cells(j + 2, 3).Value = record(j)
  Next j  
  wscen.Cells(1, 7).Value = wsin.Cells(i + 1, 1)

  wscen.Calculate

  record(1) = wscen.Cells(18, 10).Value
  record(2) = wscen.Cells(19, 10).Value
  record(3) = wscen.Cells(20, 10).Value
  record(4) = wscen.Cells(18, 11).Value
  record(5) = wscen.Cells(19, 11).Value
  record(6) = wscen.Cells(20, 11).Value
  record(7) = wscen.Cells(18, 12).Value
  record(8) = wscen.Cells(19, 12).Value
  record(9) = wscen.Cells(20, 12).Value

  wsout.Cells(i + 1, 1).Value = ref  
  For k = 1 To 9  
    wsout.Cells(i + 1, k + 1).Value = record(k)
  Next k  
Next i

End Sub

0 个答案:

没有答案