SI正在尝试将工作簿设置为数组。运行时错误9。 这是代码。
Dim geneid() As String
Dim venousdata As Workbook
Dim x As Integer
Dim pt() As Workbook
Set venousdata = Workbooks.Open("")
Set pt(1) = Workbooks.Open("")
Set pt(2) = Workbooks.Open("")
Set pt(3) = Workbooks.Open("")
Set pt(4) = Workbooks.Open("")
Set pt(5) = Workbooks.Open("")
For i = 1 To 5
pt(i).Cells(5621, 10).Value = venousdata.Cells(i + 1, 6).Value
pt(i).Cells(5621, 11).Value = venousdata.Cells(i + 1, 7).Value
pt(i).Cells(5621, 12).Value = venousdata.Cells(i + 1, 8).Value
Next i
答案 0 :(得分:1)
Set venousdata = Workbooks.Open("C:\TestFolder\B1.xls")
Cells
的值时,您应该引用工作表和工作簿:此代码将失败:
MsgBox ActiveWorkbook.Cells(1, 1).Value
答案 1 :(得分:1)
在评论和答案中似乎有部分答案,所以我决定在董事会上给出一个完整的答案。
请参阅下面的代码(只要您对数据/系统进行了编辑)。
Dim geneid() As String
Dim venousdata As Workbook
Dim i As Integer
Dim pt(1 to 5) As Workbook 'need to set the dimensions of the array before adding something to it
'you need to place which file you want to open
'change all workbook paths and references as needed
Set venousdata = Workbooks.Open("C:\Temp\Book.xlsx")
Set pt(1) = Workbooks.Open("C:\Temp\Book1.xlsx")
Set pt(2) = Workbooks.Open("C:\Temp\Book2.xlsx")
Set pt(3) = Workbooks.Open("C:\Temp\Book3.xlsx")
Set pt(4) = Workbooks.Open("C:\Temp\Book4.xlsx")
Set pt(5) = Workbooks.Open("C:\Temp\Book5.xlsx")
For i = 1 To 5
'the Cells object is a member of the Worksheet
'Object, which is a member of the Workbook Object,
'so you must set parentage in that order
'change Sheet1 to the specific sheet for your needs
pt(i).Worksheets("Sheet1").Cells(5621, 10).Value = venousdata.Worksheets("Sheet1").Cells(i + 1, 6).Value
pt(i).Worksheets("Sheet1").Cells(5621, 11).Value = venousdata.Worksheets("Sheet1").Cells(i + 1, 7).Value
pt(i).Worksheets("Sheet1").Cells(5621, 12).Value = venousdata.Worksheets("Sheet1").Cells(i + 1, 8).Value
Next i