|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 导入()
Application.ScreenUpdating = False
Dim ar As Variant, br As Variant
Dim i As Long, r As Long, rs As Long
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set sht = Sheets("sheet1")
f = Dir(ThisWorkbook.Path & "\源数据*.xls*")
If f = "" Then MsgBox "找不到源数据文件!": End
With sht
r = .Cells(Rows.Count, 1).End(xlUp).Row
y = .Cells(2, Columns.Count).End(xlToLeft).Column
If r >= 3 Then .Range(.Cells(3, 1), .Cells(r, y)) = Empty
ar = .Range(.Cells(2, 1), .Cells(100000, y))
For j = 3 To UBound(ar, 2)
If Trim(ar(1, j)) <> "" Then
d(Trim(ar(1, j))) = j
End If
Next j
n = 1
Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & f, 0)
With wb.Worksheets(1)
br = .UsedRange.Offset(3)
End With
wb.Close False
For i = 2 To UBound(br)
If Trim(br(i, 2)) <> "" Then
n = n + 1
ar(n, 1) = n - 1
ar(n, 2) = br(i, 2)
For j = 2 To UBound(br, 2)
If Trim(br(1, j)) <> "" Then
lh = d(Trim(br(1, j)))
If lh <> "" Then
ar(n, lh) = br(i, j)
End If
End If
Next j
End If
Next i
If n = 1 Then MsgBox "源数据为空!": End
.Cells(2, 1).Resize(n, UBound(ar, 2)) = ar
End With
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|