简体   繁体   English

Excel VBA无效限定符错误

[英]Excel vba invalid qualifier error

I am trying the following vba code: 我正在尝试以下vba代码:

Private Sub CommandButton1_Click()
    Dim arr() As Variant
    Dim i As Integer

    Workbooks("Contractor Manpower Tracking_NE_02.06.2015.xlsx").Activate

    arr = Sheets("NE_Scheme").Range("I3:I89").Value
    Workbooks("Scheme.xltm").Activate
    For i = 1 To arr.Length
       Cells(i, 4) = arr(i)
    Next i

End Sub

I am getting the compile error: invalid qualifier . 我收到了编译错误: invalid qualifier

EDIT 编辑

Private Sub CommandButton1_Click()
  Dim arr() As Variant
  Dim i As Integer

  Dim wb as WorkBook
  Dim str as String
  set wb=Workbooks("Contractor Manpower Tracking_NE_02.06.2015.xlsx")
  If wb is nothing then
  MsgBox "Workbook Contractor Manpower Tracking_NE_02.06.2015.xlsx not found"
  Else
   Dim abc

   For Each abc In wb.Sheets
   If abc.Name="NE_Scheme" then
    str=abc.Name
    Exit For
   End If
   Next
  If str="" then 
   MsgBox "No sheet called NE_Scheme in the current workbook"
   Exit Sub
  Else
   arr = wb.Sheets("NE_Scheme").Range("I3:I89").Value
  End If
  End If

  Workbooks("Scheme.xltm").Activate
  'Need sheet name here to write
  For i = 1 To Ubound(arr)
       Cells(i, 4) = arr(i,1)
       Next i


  End Sub

Added arr(i,1), since you can access the value of the variant only like arr(i,1) and added Ubound instead of Length 添加了arr(i,1),因为您只能像arr(i,1)一样访问变量的值,并添加了Ubound而不是Length

I think you want something like this: 我想你想要这样的东西:

Sub test()
    Dim arr() As Variant
    Dim i As Integer

    Workbooks("Book2.xlsx").Activate

    arr = ActiveWorkbook.Sheets("Sheet1").Range("I3:I89").Value
    Workbooks("Book1.xltm").Activate
    For Row = 1 To UBound(arr, 1)
        For Col = 1 To UBound(arr, 2)
            ActiveWorkbook.Worksheets("Sheet1").Cells(Row, 4) = arr(Row, Col)
        Next Col
    Next Row

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM