调整和转置数组 - 1004错误

时间:2018-04-12 15:05:28

标签: excel vba excel-vba

在Excel VBA调试器中,我收到1004应用程序定义或对象定义的错误。带有错误的行标有下面的注释。我相信.Range(Cells(2,j + 1))对象导致错误,但我相信我的语法是正确的。所以,我不确定为什么会发生这种错误。

Option Explicit

Sub Macro2()

Dim i as Integer, j As Integer, lrow As Long, rng As Range, cell As Range, size as Integer

Sheets("Std. BOMs").Select
size = Application.WorksheetFunction.CountIf(Range("F3:F999"), "Total")
ReDim arrVal(1 To size) As Long
lrow = Cells(Rows.Count, "F").End(x1Up).Row

set rng = Range("F3:F" & lrow)

For j = 0 To 1991
  j = j + 1
  ActiveSheet.Range(ActiveSheet.Cells(j,23), ActiveSheet.Cells(j,24)).Select
  Selection.Copy
  Range("E1:F1").Select
  ActiveSheet.Paste
  ActiveSheeet.Cells(j,25).Select
  Selection.Copy
  Range("G1").Select
  ActiveSheet.Paste
     i = 0
     For Each cell In rng
         If Application.WorksheetFunction.ISNA(cell) Then
              Exit For
         ElseIF InStr(1, cell.Value, "Total", vbTextCompare) > 0 Then
              i = i + 1
              arrVal(i) = cell.Offset(0,1).Value
         End If
     Next cell
   Sheets("Matrix").Range(Cells(2, j + 1)).Resize(i, 1).Value = Application.Transpose(arrVal)        '---------Line with 1004 Error----------'
  Next j
  Application.CutCopyMode = False
  ActiveWindow.ScrollRow = 1
  Range("H7").Select

End Sub

1 个答案:

答案 0 :(得分:0)

尝试将该行更改为

Sheets("Matrix").Cells(2, j + 1)).Resize(i, 1).Value = Application.Transpose(arrVal)

Range()需要2 Cells