|
[广告] 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(), a%
Set wb1 = ThisWorkbook
Set sh1 = wb1.Sheets("1-1全部列字段合并")
Set sh2 = wb1.Sheets("1-2指定列字段合并")
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)) = UBound(r1, 2) + 1
sh1.Cells(1, UBound(r1, 2) + 1) = r2(3, j)
End If
r3(i - 3, d(r2(3, j))) = r2(i, j)
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
|
|