- ©«¤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 ©ó 2022-12-23 09:11 ½s¿è
¦^´_ 4# mdr0465
ÁÂÁ«e½ú¦^´_
¤µ¤Ñ½Æ²ßקï¤F¤@¤U,½Ð«e½ú¦A¸Õ¸Õ¬Ý,¤ß±oµù¸Ñ½Ð°Ñ¦Ò
½Ð¦U¦ì«e½ú«ü¾É,ÁÂÁÂ
°õ¦æµ²ªG:
Option Explicit
Sub ¶µ¬Û¤ÀÃþ«¾ã_20221222_1()
Application.DisplayAlerts = False
'¡ô¤£n°Ý¬O¤£¬O¯uªºn§R°£¤u§@ªí!°®¯ÜÂI!
Application.ScreenUpdating = False
'¡ô¿Ã¹õ¤£n¸òµÛµ{§Ç°µÅܤÆ!°½°½°µ´N¦n¤F
Dim i&, j&, N&, St$, Arr, Brr, Y, Z, Ra, Sh
'¡ô«Å§iÅܼÆ:(i,j,N)¬Oªø¾ã¼ÆÅܼÆ,St¬O¦r¦êÅܼÆ,¨ä¥L¬O³q¥Î«¬ÅܼÆ
Set Y = CreateObject("Scripting.Dictionary")
Set Z = CreateObject("Scripting.Dictionary")
'¡ô¥OY,Z¦U¬O ¦r¨å
Set Sh = Sheets("µ²ªG")
'¡ô¥OSh¬O "µ²ªG"¤u§@ªí
Arr = Range([¤ÀÃþ±b!H1], [¤ÀÃþ±b!A1].Cells(Rows.Count, 1).End(xlUp))
'¡ô¥OArr¬O ¤Gºû°}¦C!ˤJ±q "¤ÀÃþ±b"¤u§@ªíªº[H1]¨ì¸ÓªíªºAÄæ³Ì«á¤@Ó¦³¤º®eÀx¦s®æ¤§¶¡,
'ÂX®i¦¨¬°³Ì¤p¤è¥¿°Ï°ìÀx¦s®æªºÈ
With Sheets.Add
'¡ô¥H¤U¬O¦³Ãö©ó·s¼W¤u§@ªíªºµ{§Ç
With .[A1].Resize(UBound(Arr), UBound(Arr, 2))
'¡ô¥H¤U¬O¦³Ãö©ó·s¼W¤u§@ªí±q[A1]ÂX®iÁa¦VArr°}¦C³Ì¤j¦C¸¹¼Æ,¾î¦VArr°}¦C³Ì¤jÄ渹¼Æ,
'³o¨ÇÀx¦s®æªºµ{§Ç
.Value = Arr
'¡ôÀx¦s®æÈ¥H Arr°}¦CÈ˶i¥h
.Sort _
KEY1:=.Item(1), Order1:=xlAscending, _
Key2:=.Item(2), Order2:=xlAscending, _
Header:=xlYes, Orientation:=xlTopToBottom
'¡ô¥O¥H²Ä1Äæ°µ²Ä¤@¼h°µ¦³¼Ð¦Cªº¤W¤U¶¶±Æ§Ç,²Ä2Äæ¦P®É°µ²Ä¤G¼h¤W¤U¶¶±Æ§Ç
Arr = .Value
'¡ô¥OArr°}¦C˱¼ì¨ÓªºÈ,¸Ë¤J³o±Æ§Ç¦nªºÀx¦s®æÈ
End With
.Delete
'¡ô¥O³o·s¼W¤u§@ªí§R°£
End With
'§Ú·Q±N©Ò¦³ªº" ¥»¤é¦Xp" ©M"¥»¦~²Öp" ³£§R°£
St = "/¥»¤é¦Xp/¥»¦~²Öp/ÃöÁä¦r|/ÃöÁä¦r|/"
'¡ô¥OSt³o¦r¦êÅܼƬOÂù¤Þ¸¹¸Ìªº³o¨Ç¦r,ÃöÁä¦r|¬O¥Î¨Óµ¹¨Ï¥ÎªÌ°l¥[ªº
For i = 1 To UBound(Arr)
'¡ô³]¶¶°j°é!i±q1¨ìArr°}¦C³Ì¤j¦C¸¹¼Æ
Y(Arr(i, 1)) = ""
'¡ô¥O¥H°j°é¦C²Ä1ÄæArr°}¦CÈ·íkey,item¬OªÅ¦r¤¸,©ñ¤JY¦r¨å¸Ì,
'³o¬On²Îp¦@¦³´XºØ ©ú²Ó¬ì¥Ø,¤~ª¾¹Dn¼W¥[¦h¤Ö¼ÐÃD¦C
If InStr(St, "/" & Replace(Arr(i, 4), " ", "") & "/") <> 0 Then
'¡ô¦pªG¥ÎInStr()¨ç¦¡§PÂ_¬O¤£µ¥©ó 0,«ç»ò§PÂ_?
'¥ý¥ÎReplace()¨ç¦¡±Ni°j°é¦C²Ä4ÄæArr°}¦CÈ,¥Î""ªÅ¦r¤¸¸m´«±¼" "ªÅ¥Õ¦r¤¸,
'¦A¥Î"/"²Å¸¹¦b«e«á¥]¦í³o¦r¦ê,¥H§K»~§P
'¥h¤ñ¹ïSt¦r¦êÅܼƸ̦³¨S¦³¥]§t³o¦ê¦r
'https://learn.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/instr-function
Z("¦Xp²Öp") = Z("¦Xp²Öp") + 1
'¡ô¥O¥H"¦Xp²Öp"¦r¦ê·íkey,item²Ö¥[ 1
End If
Next
ReDim Brr(1 To UBound(Arr) + Y.Count * 3 - 1 - Z("¦Xp²Öp"), 1 To UBound(Arr, 2))
'¡ô«Å§iBrr°}¦Cªº½d³ò¤j¤p,Áa¦V±q1¨ìArr°}¦C³Ì¤j¯Á¤Þ¦C¸¹¼Æ+Y¦r¨åkey¼Æ¶q*3,´î1,
'¦A´î±¼ "¦Xp²Öp"¦r¦ê·íkey¬dZ¦r¨å±o¨ìªºitemÈ
'¾î¦V±q1¨ìArr°}¦C³Ì¤j¯Á¤ÞÄ渹¼Æ
Set Ra = Sh.[A1:H1]
'¡ô¥ORa³o³q¥Î«¬ÅܼƬO Sh¤u§@ªíÅܼƸ̪º[A1:H1]Àx¦s®æ
For i = 2 To UBound(Arr)
'¡ô³]¥~¶¶°j°é!i±q2¨ìArr°}¦C³Ì¤j¦C¸¹¼Æ
If Arr(i, 1) <> Arr(i - 1, 1) Then
'¡ô¦pªGi°j°é¦C²Ä1ÄæArr°}¦CÈ ¤£µ¥©ó(i-1)°j°é¦C²Ä1ÄæArr°}¦CÈ
N = IIf(i = 2, N + 1, N + 2)
'¡ô¥ON³oªø¾ã¼ÆÅܼƪºÈ¥Î IIf()¨ç¦¡¨M©w,
'¦pªGi°j°é¼Æ¬O 2®ÉN = N + 1,§_«hN = N + 2
Set Ra = Union(Ra, Range(Sh.Cells(N, 1), Sh.Cells(N, 8)))
'¡ô¥ORa³o³q¥Î«¬ÅܼƥÎUnion()¨ç¦¡ ²Ö¿nÀx¦s®æ¶°,
'³W«h¬ORa¦Û¨ ¦A¥[¤J ±q"µ²ªG"¤u§@ªí N¦C²Ä1ÄæÀx¦s®æ¨ì ¸ÓªíN¦C²Ä8ÄæÀx¦s®æ,
'³o¨â®æ¤§¶¡ªº©Ò¦³Àx¦s®æ
Brr(N, 2) = Arr(i, 1)
'¡ô¥ONÅܼƦC²Ä2ÄæBrr°}¦CȬO i°j°é¦C²Ä1ÄæArr°}¦CÈ (©ú²Ó¬ì¥Ø)
N = N + 1
'¡ô¥ON³oªø¾ã¼ÆÅܼÆX²Ö¥[ 1 (¥[1¦C)
Set Ra = Union(Ra, Range(Sh.Cells(N, 1), Sh.Cells(N, 8)))
'¡ô¦P¤W
For j = 1 To UBound(Arr, 2)
'¡ô³]¤º¶¶°j°é!j±q1¨ìArr°}¦C³Ì¤jÄ渹¼Æ
Brr(N, j) = Arr(1, j)
'¡ô¥ONÅܼƦC²Äj°j°éÄæBrr°}¦CȬO ²Ä1¦C²Äj°j°éÄæArr°}¦CÈ
Next
End If
If InStr(St, "/" & Replace(Arr(i, 4), " ", "") & "/") <> 0 Then
'¡ô¦P¤W
GoTo Hi
'¡ô´N¥h§ä Hi
End If
N = N + 1
'¡ô¦P¤W
Set Ra = Union(Ra, Range(Sh.Cells(N, 1), Sh.Cells(N, 8)))
'¡ô¦P¤W
For j = 1 To UBound(Arr, 2)
'¡ô¦P¤W
Brr(N, j) = Arr(i, j)
'¡ô¥ONÅܼƦC²Äj°j°éÄæBrr°}¦CȬO ²Äi°j°é²Äj°j°éÄæArr°}¦CÈ
Next
Brr(N, 2) = "'" & Format(Brr(N, 2), "yyyy-mm-dd")
'¡ô¥ONÅܼƦC²Ä2ÄæBrr°}¦CȬO"'" ²Å¸¹³s±µ¦Û¨¤é´ÁÂର¦r¦ê,
'¥H"yyyy-mm-dd"¤è¦¡§e²{
Brr(N, 3) = "'" & Brr(N, 3)
'¡ô¥ONÅܼƦC²Ä3ÄæBrr°}¦CȬO"'" ²Å¸¹³s±µ¦Û¨
Hi:
'Hi¦b³o¸Ì
Next
Sh.UsedRange.ClearContents
'¡ô¥OShÅܼƤu§@ªí¦³¨Ï¥ÎªºÀx¦s®æ³Ì¤p¤è¥¿°Ï°ìÀx¦s®æ¤º®e²M°£
Sh.Cells.Borders.LineStyle = 0
'¡ô¥OShÅܼƤu§@ªí¥þ³¡ªº®æ½u³£¤£n
Ra.Borders.LineStyle = 1
'¡ô¥ORa³oÀx¦s®æ¶°ªº®æ½u¬O ²Ó¹ê½u
Sh.[A1].Resize(UBound(Brr), UBound(Brr, 2)) = Brr
'¡ô¥OShÅܼƤu§@ªí±q[A1]ÂX®iÁa¦V:Brr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹¼Æ,
'¾î¦V:Brr°}¦C¾î¦V³Ì¤j¯Á¤ÞÄ渹¼Æ,³o½d³òÀx¦s®æ¥H Brr°}¦CÈˤJ
'§¹¤u¤F
Set Y = Nothing
Set Z = Nothing
Set Arr = Nothing
Set Brr = Nothing
'¤u¨ã®e¾¹n¦¬¤@¦¬,ÄÀ©ñ±¼ÅܼÆ
End Sub |
|