我正在尝试以下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
我收到编译错误:invalid qualifier
。
答案 0 :(得分:0)
修改强>
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
添加了arr(i,1),因为你只能像arr(i,1)那样访问变量的值 并添加了Ubound而不是Length
答案 1 :(得分:0)
我想你想要这样的东西:
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