|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 全部列字段合并()'修改:不用手工填写列标题
Dim w$, wb As Workbook, wb1 As Workbook, sh As Worksheet, i%, j%
Dim d As Object, sh1 As Worksheet, sh2 As Worksheet, a%, b%
Dim r1, r2, r3()
Set wb1 = ThisWorkbook
Set sh1 = wb1.Sheets("1-1全部列字段合并")
sh1.UsedRange.ClearContents
sh1.[a1].Value = "工作簿名": sh1.[b1].Value = "工作表名"
Set d = CreateObject("Scripting.Dictionary")
w = Dir(wb1.Path & "\*.xlsx")
Do While w <> ""
a = sh1.Range("d" & Rows.Count).End(xlUp).Row + 1
b = sh1.Range("a1").End(xlToRight).Column
r1 = sh1.Range(Cells(1, 1), Cells(1, b))
For j = 3 To UBound(r1, 2)
d(r1(1, j)) = j
Next j
Set wb = Workbooks.Open(wb1.Path & "\" & w)
Set sh = wb.Sheets(1)
r2 = sh.Range("a3").CurrentRegion
ReDim r3(1 To UBound(r2), 1 To 50)
For i = 3 To UBound(r2)
If r2(i, 2) = r2(4, 2) Then
For j = 1 To UBound(r2, 2)
If d(r2(3, j)) = "" Then
d(r2(3, j)) = b + 1
sh1.Cells(1, b + 1) = r2(3, j)
r3(i - 3, d(r2(3, j))) = r2(i, j)
b = sh1.Range("a1").End(xlToRight).Column
Else
r3(i - 3, d(r2(3, j))) = r2(i, j)
End If
Next j
End If
Next i
r3(1, 1) = Left(w, Len(w) - 5): r3(1, 2) = sh.Name
wb.Close 0
sh1.Range("a" & a).Resize(UBound(r3), UBound(r3, 2)) = r3
w = Dir
Loop
End Sub |
评分
-
1
查看全部评分
-
|