Arrays 在Excel VBA中不使用循环显示数组
显示以下代码的结果时出现问题:Arrays 在Excel VBA中不使用循环显示数组,arrays,vba,excel,Arrays,Vba,Excel,显示以下代码的结果时出现问题: For i = 1 To UBound(files) Step 1 Workbooks.Open files(i) With ActiveWorkbook Module2.testfunction thisWb.Sheets(1).Range("A1:C" & Ubound(testfunction)) = testfunction .Close False End With
For i = 1 To UBound(files) Step 1
Workbooks.Open files(i)
With ActiveWorkbook
Module2.testfunction
thisWb.Sheets(1).Range("A1:C" & Ubound(testfunction)) = testfunction
.Close False
End With
Next i
在for循环的每个迭代中,都有一个新的工作簿,我将“testfunction”应用于该工作簿。此函数返回一个数组,我需要将此数组写入运行宏的工作簿(“thisWb”)。每次迭代都有一个新数组,这个新数组必须显示在下一列中,以免与之前显示的数组重叠(间距为一列)
正如现在的代码一样,存在重叠。我想找到一种方法来迭代范围(在列部分),而不必借助于带有Cells()的循环
可能吗?试试这个(未测试)
试试这个(未经测试)
嗯cells()
将是合乎逻辑的选择,但可能是偏移量offset
可能?@findwindowcells()
是我的第一个想法,但要显示单个数组需要花费很长时间。这就是为什么我想使用range()
。由于我是VBA新手,我不知道offset
函数,但它可能会工作。嗯cells()
将是合乎逻辑的选择,但可能是偏移量offset
可能?@findwindowcells()
是我的第一个想法,但要显示单个数组需要花费很长时间。这就是为什么我想使用range()
。由于我是VBA新手,我不知道offset
函数,但它可能会工作。
Dim wb As Workbook, v As Variant, lCol As Long
For i = 1 To UBound(Files)
Set wb = Workbooks.Open(Files(i))
With thisWb.Sheets(1)
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column + 2
v = testfunction
.Range(.Cells(1, lCol), .Cells(UBound(v, 1), UBound(v, 2) + lCol)) = v
wb.Close False
End With
Next