- ©«¤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
|
ÁÂÁ½׾Â,ÁÂÁ¦U¦ì«e½ú
«á¾Ç¤µ¤Ñ½Æ²ß#2¼Óªº¾Ç²ß¤è®×,¤è®×¤ß±oµù¸Ñ¦p¤U,½Ð¦U¦ì«e½ú«ü±Ð
Option Explicit
Sub TEST()
Dim Brr, Crr, V, Y, R, T, i&, j&, Td$, Tn$, G1$
Dim xR As Range, xU As Range, Sh As Worksheet, MyBook, MyPath
'¡ô«Å§iÅܼÆ:(Brr,Crr,V,Y,R,T)¬O³q¥Î«¬ÅܼÆ,(i,j)¬Oªø¾ã¼Æ,(Td,Tn,G1)¬O¦r¦êÅܼÆ
',(xR,xU)¬OÀx¦s®æÅܼÆ,Sh¬O¤u§@ªíÅܼÆ,(MyBook,MyPath)¬O³q¥Î«¬ÅܼÆ
Set Y = CreateObject("Scripting.Dictionary")
'¡ô¥OY³o³q¥Î«¬ÅܼƬO ¦r¨å
Set Sh = Sheets("¸ËÂd³qª¾")
'¡ô¥OSh³oÀx¦s®æÅܼƬO ¦W¬°"¸ËÂd³qª¾"ªº¤u§@ªí
Set R = Sh.[D:D].Find("TOTAL", Lookat:=xlWhole)
'¡ô¥OR³o³q¥Î«¬ÅܼƬO ¥HRange.Find¤èªk§äDÄ椤Àx¦s®æȬO¥þ¦P "TOTAL"¦r¦ê
'https://learn.microsoft.com/zh-tw/office/vba/api/excel.range.find
If Not R Is Nothing Then R = R.Row Else Exit Sub
'¡ô¦pªGRÅܼƧä¨ìÀx¦s®æ!´N¥OR³oÅܼƴ«¸ËRÅܼÆ(Àx¦s®æ)ªº¦C¸¹,
'§_«h´Nµ²§ôµ{¦¡°õ¦æ
Brr = Range(Sh.[A7], Sh.Cells(R - 1, "H"))
'¡ô¥OBrr³o³q¥Î«¬ÅܼƬO ¤Gºû°}¦C,¥H[A7]¨ìHÄæ(RÅܼÆ-1)¦CÀx¦s®æȱa¤J
For i = 1 To UBound(Brr)
'¡ô³]¶¶°j°é!i±q1¨ì Brr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹
If Brr(i, 1) = "" Then
'¡ô¦pªGi°j°é¦C²Ä1ÄæBrr°}¦CȬO ªÅ¦r¤¸
For j = 1 To 8: T = T & Trim(Brr(i, j)): Next
'¡ô³]¶¶°j°é!j±q1¨ì 8,¥OT³o¦r¦êÅܼƬO¦Û¨³s±µ,
'(i°j°é¦Cj°j°éÄæBrr°}¦CÈ)¥h°£ÀY§ÀªÅ¥Õ¦r¤¸«á²Õ¦¨ªº·s¦r¦ê
If T <> "" Then Brr(i, 1) = Brr(i - 1, 1) Else: GoTo i01
'¡ô¦pªGTÅܼƤ£¬OªÅ¦r¤¸!´N¥Oi°j°é¦C²Ä1ÄæBrr°}¦CȬO¤W¤@¦C°}¦CÈ
'§_«h´N¸õ¨ìi01¼Ð¥Ü¦ì¸mÄ~Äò°õ¦æ
End If
Y(Brr(i, 1)) = Y(Brr(i, 1)) + 1
'¡ô¥Oi°j°é¦C²Ä1ÄæBrr°}¦CÈ·íkey,item¬Oitem¦Û¨²Ö¥[1
If Y(Brr(i, 1) & "|c") = "" Then Y(Brr(i, 1) & "|c") = Brr(i, 3)
'¡ô¦pªG¥Oi°j°é¦C²Ä1ÄæBrr°}¦CÈ·íkey¬dY¦r¨å±oitemȬO ªÅ¦r¤¸?
'´N¥O¥Oi°j°é¦C²Ä1ÄæBrr°}¦Cȳs±µ "|c"«áªº·s¦r¦ê·íkey,item¬Oi°j°é¦C²Ä3ÄæBrr°}¦CÈ
i01: T = "": Next
If Y.Count = 0 Then Exit Sub
'¡ô¦pªGY¦r¨åkeyªºÓ¼Æ¬O 0Ó!´Nµ²§ôµ{¦¡°õ¦æ
G1 = Sh.[G1].Text & Sh.[E1] & "-" & Replace(Replace(Sh.[G2], "/", ""), "#", "") & Sh.[H2]
'¡ô¥OG1³o¦r¦êÅܼƬO [G1]Àx¦s®æÅé²{ªº¦rÂন¤å¦r ³s±µ[E1]Àx¦s®æÈ,³s±µ"-",
'¦A³s±µ([G2]Àx¦s®æ¥h°£"/"¦r¤¸»P"#"¦r¤¸),³Ì«á³s±µ[H2]Àx¦s®æÈ
Td = Format(Now, "YYYY_MM_DD_HH_MM_SS")
'¡ô¥OTd³o¦r¦êÅܼƬO ²{¦b®É¶¡Âন¤å¦r
Set MyBook = ThisWorkbook
'¡ô¥OMyBook³o³q¥Î«¬ÅܼƬO ¥»¬¡¶Ã¯
MyPath = MyBook.Path & "\"
'¡ô¥OMyPath³o³q¥Î«¬ÅܼƬO ¥»¬¡¶Ã¯¸ô®| ³s±µ"\"«áªº·s¦r¦ê
MkDir MyPath & Td
'¡ô¥O²£¥Í¤@Ó¸ê®Æ§¨,¦W¦r¬O TD,¸ô®|¦bMyPath
For Each T In Y.KEYS
'¡ô³]³v¶µ°j°é!¥OTÅܼƬO Y¦r¨å¸Ìªº¤@Ókey
If InStr(T, "|") Then GoTo i02
'¡ô¦pªGTÅܼƸ̦³¥]§t"|"¦r¤¸!´N¸õ¨ìi02¼Ð¥Ü¦ì¸mÄ~Äò°õ¦æ
Sh.Copy
'¡ô¥OShÅܼÆ("¸ËÂd³qª¾"¤u§@ªí)½Æ»s¨ì·s¶}ªº¬¡¶Ã¯
Set xU = Cells(Rows.Count, 1).Resize(1, 8)
'¡ô¥OxU³oÀx¦s®æÅܼƬO¥»ªíAÄæ³Ì«á¦CÀx¦s®æ,¦V¥kÂX®i8®æ½d³òªºÀx¦s®æ
For i = 1 To UBound(Brr)
'¡ô¥O³]¶¶°j°é!i±q1¨ì Brr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹
If Brr(i, 1) <> T Then
'¡ô¦pªGi°j°é¦C²Ä1ÄæBrr°}¦CÈ »P ÅܼƤ£¦P?
Set xU = Union(Cells(i + 6, 1).Resize(1, 8), xU)
'¡ô¥OxUÅܼÆÄ~Äò¥HUnion()¤èªk¯Ç¤J¤£nªºÀx¦s®æ
End If
Next
xU.Delete: [A7] = 1
'¡ô¥OxUÅܼÆ(Àx¦s®æ)§R°£,¥O[A7]Àx¦s®æȬO 1
Tn = Y(T & "|c") & "-" & G1 & ".xlsx"
'¡ô¥OTn³o¦r¦êÅÜ¼Æ ¬O²Õ¦X¦U¥²nÅܼƲզ¨ªº·s¦r¦ê
ActiveWorkbook.SaveAs Filename:=MyPath & Td & "\" & Tn, _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
'¡ô¥OÀÉ®×¨Ì «ü©w¦ì¸m «ü©wÀɦW Àx¦s
ActiveWindow.Close
'¡ôÃö³¬ÀÉ®×
i02: Next
Set Y = Nothing: Set Sh = Nothing: Erase Brr
'¡ô¥OÄÀ©ñÅܼÆ
End Sub |
|