|
类似应用,从不同工作薄取数,并合并成一个数组,其它操作以这个合并数组为数据源进行各项其它操作:
Sub unarr()
Dim arr, brr(), crr, drr, uArr
Dim fn, i, j, k1, k2, m, n
Dim a, b, c, d, e, f
'On Error Resume Next
Dim MyPath$, MyName$, sh As Worksheet
Application.ScreenUpdating = False
crr = Array("物料代码", "物料名称", "规格型号", "仓库名称", "常用计量单位", "常用单位数量") ‘’表头,以及取其中指定列的数据
Set sh = ActiveSheet
sh.Cells.Clear
'For n = 0 To UBound(crr) '写表头
' sh.Cells(1, n + 1) = crr(n)
'Next
MyPath = ThisWorkbook.Path & "\"
MyName = Dir(MyPath & "*.xls")
Do While MyName <> ""
If MyName <> ThisWorkbook.Name Then
fn = fn + 1
With GetObject(MyPath & MyName)
arr = .Sheets("即时库存").Range("A1").CurrentRegion
For k1 = 1 To UBound(arr, 2)
If arr(1, k1) = crr(0) Then
a = k1
ElseIf arr(1, k1) = crr(1) Then
b = k1
ElseIf arr(1, k1) = crr(2) Then
c = k1
ElseIf arr(1, k1) = crr(3) Then
d = k1
ElseIf arr(1, k1) = crr(4) Then
e = k1
ElseIf arr(1, k1) = crr(5) Then
f = k1
End If
Next
ReDim brr(1 To UBound(arr), 1 To 6) '将指定列的内容赋值给数组
For m = 1 To UBound(arr)
brr(m, 1) = arr(m, a)
brr(m, 2) = arr(m, b)
brr(m, 3) = arr(m, c)
brr(m, 4) = arr(m, d)
brr(m, 5) = arr(m, e)
brr(m, 6) = arr(m, f)
Next
If fn = 1 Then
sh.[A1].Resize(UBound(brr), UBound(brr, 2)) = brr
Else
sh.[A65536].End(xlUp).Offset(1).Resize(UBound(brr), UBound(brr, 2)) = brr '将各工作薄数据写入当前工作表
End If
.Close False
End With
End If
MyName = Dir
Loop
With sh
.Cells.Font.Size = 10
.Cells.Replace Chr(10), ""
uArr = .Range("A1").CurrentRegion '将所有合并数据再赋值给 Uarr ——这才是最终想得到的数组
End With
Application.ScreenUpdating = True
End Sub
上面代码是写到工作表内(用Copy方法也一样)。能否直接通过后台的数组合并操作而得到 Uarr ?
|
|