- ©«¤l
- 1446
- ¥DÃD
- 40
- ºëµØ
- 0
- ¿n¤À
- 1470
- ÂI¦W
- 0
- §@·~¨t²Î
- Windows 7
- ³nÅ骩¥»
- Excel 2010 & 2016
- ¾\ŪÅv
- 50
- ©Ê§O
- ¨k
- ¨Ó¦Û
- ¥xÆW
- µù¥U®É¶¡
- 2020-7-15
- ³Ì«áµn¿ý
- 2024-10-21
|
¥»©«³Ì«á¥Ñ Andy2483 ©ó 2023-2-13 11:57 ½s¿è
¦^´_ 1# rcyw
¦^´_ 2# ã´£³¡ªL
ÁÂÁ rcyw«e½úµoªí¦¹¥DÃD»P½d¨Ò,ÁÂÁ 㴣³¡ªL«e½ú½d¨Ò«ü¾É
¾Ç²ß¤ß±oµù¸Ñ¦p¤U,½Ð«e½ú¦A«ü¾É
Sub combine()
Dim Arr, Brr, PH$, FN$, xB As Workbook, xS As Worksheet, i&, N&
'¡ô«Å§iÅܼÆ!(Arr,Brr)¬O³q¥Î«¬ÅܼÆ,(PH,FN)¬O¦r¦êÅܼÆ,xB¬O¬¡¶Ã¯ÅܼÆ,
'xS¬O¤u§@ªíÅܼÆ,(i,N)¬Oªø¾ã¼ÆÅܼÆ
ReDim Brr(1 To 60000, 0)
'¡ô«Å§iBrr¬°¤Gºû°}¦C,°}¦C¤j¤p:Áa¦V±q1¯Á¤Þ¸¹¦C ¨ì60000¯Á¤Þ¸¹¦C,
'¾î¦V±q0¯Á¤Þ¸¹Äæ ¨ì 0¯Á¤Þ¸¹Äæ
Application.ScreenUpdating = False
'¡ô¥O¿Ã¹õ¼È¤£ÀHµ{¦¡°õ¦æ§@ÅܤÆ
PH = ThisWorkbook.Path & "\TEST"
'¡ô¥OPH³o¦r¦êÅܼƬO ¥»Àɮתº§¹¾ã¸ô®|¦r¦ê³s±µ "\TEST" ªº·s¦r¦ê
'https://learn.microsoft.com/zh-tw/office/vba/api/excel.workbook.path
FN = Dir(PH & "\*.xls*")
'¡ô¥ODir ¨ç¼Æ¦^¶Ç (¸ô®|»PÀÉ®×Ãþ«¬:PHÅܼƳs±µ "\*.xls*" )µ¹FN³o¦r¦êÅܼÆ
Do While FN <> ""
'¡ô³]±ø¥ó°j°é!·íFNÅܼƤ£¬OªÅ¦r¤¸®É,Ä~Äò°õ¦æ
Set xB = Workbooks.Open(PH & "\" & FN)
'¡ô¶}±Ò(PHÅÜ¼Æ ³s±µ "\" & FNÅܼƲզX¦¨ªº·s¦r¦ê¸ô®|ÀÉ®×,¨Ã¥OxB³o¬¡¶Ã¯ÅܼƬO¥L
For Each xS In xB.Sheets
'¡ô³]³v¦¸°j°é!¥OxS³o¤u§@ªíÅܼƬO xBÅܼƸ̪º¤u§@ªí
If xS.Name Like "Script_*" = False Then GoTo x01
'¡ô¦pªGxSÅܼƪº¦W¦r¤£¬O ¥H "Script_" ¶}ÀYªº¦r¦ê!´N¸õ¨ì x01¼Ð¥Ü³BÄ~Äò°õ¦æ
Arr = Range(xS.[a1], xS.Cells(Rows.Count, 1).End(3)(2))
'¡ô¥OArr³o³q¥Î«¬ÅܼƬO xSÅܼÆ[A1]¨ìAÄæ³Ì«á¤@¦³¤º®eÀx¦s®æªº¤U¤@®æ(ªÅ¥Õ®æ),
'¥H³o½d³òÀx¦s®æÈˤJ ³oArr¤Gºû°}¦C¸Ì
For i = 1 To UBound(Arr) - 1
'¡ô³]¶¶°j°é!i±q1¨ì Arr°}¦CÁa¦V¯Á¤Þ¦C¸¹ -1
If Arr(i, 1) <> "" Then N = N + 1: Brr(N, 0) = Arr(i, 1)
'¡ô¦pªGi°j°é¦C/²Ä1ÄæArr°}¦CȤ£¬OªÅ¦r¤¸!´N¥ON³oªø¾ã¼ÆÅܼƲ֥[1,
'¥ONÅܼƦC0¯Á¤Þ¸¹Äæ Brr°}¦CȬO i°j°é¦C/²Ä1ÄæArr°}¦CÈ
Next i
x01: Next
xB.Close 0
'¡ô¥OxBÅܼÆ,¤£Àx¦sÃö³¬
FN = Dir
'¡ô¥OFNÅܼƬO Dirªº¤U¤@Ó¶µ¥Ø
Loop
Set xB = Nothing: Set xS = Nothing
'¡ô¥O³o¨âÓª«¥óÅܼƲMªÅ
'=============================
ThisWorkbook.Activate
'¡ô¥O¦^¨ì¥»ÀÉ
If N = 0 Then Exit Sub
'¡ô¦pªGNÅܼƬO 0!´Nµ²§ôµ{¦¡°õ¦æ
Application.DisplayAlerts = False
'¡ô¥O¤£n¦A¸õ¥X´£¥Ü:°Ý¤u§@ªí¬O¤£¬On§R°£!´Nª½±µ§R°£!¤£n¦A°Ý¤F!
'https://learn.microsoft.com/zh-tw/office/vba/api/excel.application.displayalerts
On Error Resume Next
'¡ô¥O±q¦¹³B¶}©lªºµ{§Ç¹J¨ì¿ù»~®É¤£n°»¿ù!¸õ¹L¸Óµ{§ÇÄ~Äò°õ¦æ
Sheets("Combine").Delete
'¡ô¥O "Combine"¤u§@ªí§R°£
On Error GoTo 0
'¡ô¥Oµ{§Ç±q¦¹³B¶}©l«ì´_°»¿ù
With Worksheets.Add(After:=Sheets(Sheets.Count))
'¡ô¥H¤U¬OÃö©ó¦b³Ì«á·s¼W¥[¤@Ó¤u§@ªí«áªºµ{§Ç
.[a1].Resize(N) = Brr
'¡ô¥O·s¼W¤u§@ªíªº[A1]ÂX®i¦V¤UN¦Cªº½d³òÀx¦s®æÈ,¥HBrr°}¦CÈˤJ
.Name = "Combine"
'¡ô¥O·s¼W¤u§@ªíªº¦W¦r¬O "Combine"
End With
Sheets(1).Select
'¡ô¿ï¨ú²Ä1Ó¤u§@ªí
End Sub |
|