- ©«¤l
- 1447
- ¥DÃD
- 40
- ºëµØ
- 0
- ¿n¤À
- 1471
- ÂI¦W
- 0
- §@·~¨t²Î
- Windows 7
- ³nÅ骩¥»
- Excel 2010 & 2016
- ¾\ŪÅv
- 50
- ©Ê§O
- ¨k
- ¨Ó¦Û
- ¥xÆW
- µù¥U®É¶¡
- 2020-7-15
- ³Ì«áµn¿ý
- 2024-12-5
|
¥»©«³Ì«á¥Ñ Andy2483 ©ó 2022-12-6 11:51 ½s¿è
¦^´_ 14# ã´£³¡ªL
¦A¦¸ÁÂÁ 㴣³¡ªL«e½ú«ü¾É
¦A¦¸ÁÂÁ b9208«e½úµoªí¦¹¥DÃD»P½d¨Ò
«á¾ÇÂǦ¹©«½m²ß¥H¦r¨åªº¤@ºû°}¦CITEMÂà¸m¦¨µ²ªG,±o«Ü¦hª¾ÃÑ,ÁÂÁ½׾Â
³o½d¨Ò¥Î ã´£³¡ªL«e½úªºµ{¦¡¬[ºc¦A¦h¤@Ó¦r¨å»P¤@ºû°}¦C°µÅܤÆ,µ²ªGµy¦³¤£¦P
°õ¦æ²Ó¸`»P¤ß±oµù¸Ñ¦p¤U,½Ð«e½ú¦A«ü¾É!ÁÂÁÂ
°õ¦æ«á:
Option Explicit
Sub TEST_Á`ªí_Âà¸m()
Dim Arr, Y, Z, Q, PH$, FN$, i&, j&, V$(14)
'¡ô«Å§i(Arr, Y, Z, Q)¬O³q¥Î«¬ÅܼÆ,(PH$, FN$)¬O¦r¦êÅܼÆ,(i,j)¬Oªø¾ã¼ÆÅܼÆ,
'V¬O15Ó¤¸¯Àªº¤@ºû¦r¦ê°}¦CÅܼÆ
Dim xB As Workbook, xS As Worksheet, TT$, T$(4), M&
'¡ô«Å§i(xB)¬O¬¡¶Ã¯ÅܼÆ,(xS)¬O¤u§@ªíÅܼÆ,
'(TT)¬O¦r¦êÅܼÆ,T¬O5Ó¤¸¯Àªº¤@ºû¦r¦ê°}¦CÅܼÆ,(M&)¬Oªø¾ã¼ÆÅܼÆ
Set Y = CreateObject("Scripting.Dictionary")
Set Z = CreateObject("Scripting.Dictionary")
'¡ô¥OY,Z ¦U¬O¦r¨å
PH = ThisWorkbook.Path
'¡ô¥OPH¦r¦êÅܼƬO ²{¦b³o¬¡¶Ã¯ªº©Ò¦bÀɮצì¸m
Call ²M°£_Á`ªí1
'¡ô°õ¦æ °Æµ{¦¡ ²M°£_Á`ªí1()
'--------------------------
Arr = Range([Á`ªí!a1], [Á`ªí!a65536].End(xlUp))
'¡ô¥OArr¬O°}¦C!ˤJ"Á`ªí"¤u§@ªí[A1]¨ì"Á`ªí"¤u§@ªíAÄæ³Ì«á¤@Ó¦³¤º®eªºÀx¦s®æ
For i = 2 To UBound(Arr)
'¡ô³]¶¶°j°éi±q2 ¨ìArr°}¦CÁa¦V³Ì¤j¦C¸¹
If Arr(i, 1) <> "" Then Y(Arr(i, 1) & "//a") = 1
'¡ô¦pªGArr°}¦C°j°é¦C²Ä¤@Ä檺Ȥ£¬O ªÅ¦r¤¸!
'´N¥HArr°}¦C°j°é¦C²Ä¤@ÄæªºÈ ³s±µ "//a" ¦r¦ê ·íY¦r¨åªºKEY,
'item¬O¼Æ¦r1
Next i
Application.ScreenUpdating = False
'¡ô¿Ã¹õµe±¤£¸òµÛ°õ¦æµ{§ÇÅܤÆ
Do
'¡ô³]µL°j°é!¶}©l°õ¦æ«á±ªºµ{§Ç,¦Û¤v·Q¿ìªk¸õ¥X°j°é
If FN = "" Then
'¡ô¦pªGFN¦r¦êÅܼƬO ªÅ¦r¤¸??
FN = Dir(PH & "\*.xls")
'¡ô¥OFN¦r¦êÅܼƬO ²{¦b³o¬¡¶Ã¯ªº©Ò¦bÀɮצì¸mªºEXCELÀÉ®×
Else
FN = Dir
'¡ôFN³v¦¸§ì¨ú¬Û¦P¸ô®|¤Uªº·í¦W
'https://learn.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/dir-function
End If
If FN = "" Then Exit Do
'¡ô¦pªGFN¦r¦êÅܼƬOªÅ¦r¤¸!¥Nªí¬Û¦P¸ô®|¤UªºÀɦW¤w¸g³£§ì¹L¤F,´N¸õ¥XDo~Loopªº°j°é
If FN = ThisWorkbook.Name Then GoTo DP
'¡ô¦pªGFN¦r¦êÅܼƬO ²{¦b³o¬¡¶Ã¯ªº¦WºÙ¦r¦ê!´N¸õ¨ì DP ¦ì¸mÄ~Äò°õ¦æ
'--------------------------
Set xB = Workbooks.Open(PH & "\" & FN, ReadOnly:=True)
'¡ô¥OxB ¬O¥H°ßŪ¤è¦¡¥´¶} PH¦r¦êÅÜ¼Æ & "\" & FN¦r¦êÅܼƲզX¦¨¦r¦êªº¬¡¶Ã¯
Set xS = xB.Sheets(1)
'¡ô¥OxS ¬O¥´¶}ªº³o¬¡¶Ã¯ªº²Ä¤@Ó¤u§@ªí
Arr = Range(xS.[A1:O1], xS.UsedRange)
'¡ô¥OArr˱¼ì¨Óªº°}¦CÈ!Åܦ¨·s°}¦C,ˤJ¥´¶}ªº¬¡¶Ã¯²Ä¤@Óªí¥þ³¡¦³¨Ï¥ÎªºÀx¦s®æÈ
'xS.UsedRange¦³¥i¯à¤£¥]§t³Ì¥ª¤W¨¤ªºÀx¦s®æ!©Ò¥H¥Î xS.[A1:O1]¦b«e¤è°µ§¹¾ã°Ï°ìÀx¦s®æ¦J¦C
For i = 6 To UBound(Arr)
'¡ô³]¥~¶¶°j°é!i±q6¨ìArr°}¦CÁa¦V³Ì¤j¦C¸¹
T(1) = Arr(i, 6)
'¡ô¥OT¤@ºû°}¦Cªº²Ä¤GÓ¦r¦ê¬Oi°j°é¼Æ²Ä¤»ÄæArr°}¦CÈ
T(2) = Arr(i, 8)
'¡ô¥OT¤@ºû°}¦Cªº²Ä¤TÓ¦r¦ê¬Oi°j°é¼Æ²Ä¤KÄæArr°}¦CÈ
T(3) = Left(Arr(i, 9), 7)
'¡ô¥OT¤@ºû°}¦Cªº²Ä¥|Ó¦r¦ê¬Oi°j°é¼Æ²Ä¤EÄæArr°}¦CȪº¥ªÃä7Ó¦r¤¸
T(4) = Arr(i, 3)
'¡ô¥OT¤@ºû°}¦Cªº²Ä¤Ó¦r¦ê¬Oi°j°é¼Æ²Ä¤TÄæArr°}¦CÈ
If T(1) = "" Or Y(T(1) & "//a") = 0 Then
'¡ô¦pªGT°}¦Cªº²Ä¤GÓ¦r¦ê¬O ªÅ¦r¤¸ ©Î
'¥H T°}¦Cªº²Ä¤GÓ¦r¦ê³s±µ "//a"¦r¦ê¬d¹îY¦r¨å ¬O0??
GoTo 101
'¡ô±ø¥ó¦¨¥ß!´N¸õ¨ì 101ªº¦ì¸mÄ~Äò°õ¦æ
End If
TT = T(1) & "|" & T(2) & "|" & T(3) & "|" & T(4)
'¡ô¥OTT¦r¦êÅܼƬOT°}¦C¸Ì²Ä¤GÓ¦r¦ê¨ì²Ä¤Ó¦r¦ê¤¤¶¡³s±µ"|"²Å¸¹ªº¦r¦ê
If Y.Exists(TT) = Empty Then
'¡ô¦pªG ¥ÎTT¦r¦êÅܼƬd¹îY¦r¨å¨S¦³³oÓkey
Y(TT) = Arr(i, 11)
'¡ô¥OTT¦r¦êÅܼƬOkey,item¬O°j°é¦C¼Æ²Ä11Ä檺Arr°}¦CÈ,ˤJY¦r¨å¸Ì
ElseIf Arr(i, 11) > Y(TT) Then
'¡ô§_«h¦pªG °j°é¦C¼Æ²Ä11Ä檺Arr°}¦CÈ ¤j©ó (TT·íkey¬d¹îY¦r¨åªºitemÈ)
Y(TT) = Arr(i, 11)
'¡ô¥OTT¦r¦êÅܼƬOkey,item¬O°j°é¦C¼Æ²Ä11Ä檺Arr°}¦CÈ,ˤJY¦r¨å¸Ì,
'¦pªGkey«½Æ!´N±Nitem¸m´«¦¨·sªº
Z.Remove TT
'¡ô¥OZ¦r¨å§R°£ TT¦r¦êÅܼƪºkey»Pitem
M = M - 1
'¡ô¥OM´î±¼ 1
End If
If Z.Exists(TT) = Empty Then
M = M + 1
'¡ô¥OM²Ö¥[1
Q = V
'¡ô¥OQ ¬OV¤@ºû°}¦C
For j = 0 To UBound(Q)
'¡ô³]¤º¶¶°j°é!j±q0 ¨ìQ°}¦C¾î¦V³Ì¤jÄ渹
Q(j) = Arr(i, j + 1)
'¡ô¥O j¤º°j°é¯Á¤Þ¸¹ªºQ°}¦CÈ = ¥~°j°éi¦C¸¹ j+1¤º°j°éÄ渹ªºArr°}¦CÈ
Next
Q(8) = Left(Q(8), 7)
'¡ô8¯Á¤Þ¸¹ªºQ°}¦CÈ = ¦Û¤vªºÈ¨ú¥ª°¼ªº7 Ó¦r¤¸
Z(TT) = Q
'¡ô¥OTT¦r¦êÅܼƬ°key,Item¬OQ¤@ºû°}¦C
End If
101: Next i
xB.Close 0
'¡ôÃö³¬³Q¶}±Òªº xB¬¡¶Ã¯ÅܼÆÀÉ®×(¤£¦sÀÉ)
DP: Loop
'¡ôµL°j°éªºÂà§éÂI!¸õ¨ì«e±ªºDo¦ì¸mÄ~Äò°õ¦æ!
'--------------------------
If M > 0 Then
'¡ô¦pªGM¾ã¼ÆÅܼƤj©ó0 (³o¸ÌªºM «üªº¬O Z¦r¨åkey¼Æ¶q)
With [Á`ªí!b6].Resize(M, UBound(V) + 1)
'¡ô¥H¤UÃö©ó "Á`ªí"¤u§@ªí[B6]¦V¤UÂX®iM¦C,¦V¥kÂX®iQ°}¦C³Ì¤j¯Á¤Þ¸¹+1¼ÆÄæ ªºÀx¦s®æ°Ï°ì
.Value = Application.Transpose(Application.Transpose(Z.items))
'¡ô³oÂX®i°Ï°ìÀx¦s®æ¥HZ¦r¨åªºitemsÂà¸m¨â¦¸ªºÈˤJ
.Borders.LineStyle = 1
'¡ô¥O³oÂX®i°Ï°ìÀx¦s®æªº®æ½u¼Ë¦¡¬O ²Ó¹ê½u
.Sort key1:=.Item(3), Order1:=xlAscending, _
key2:=.Item(8), Order2:=xlAscending, Header:=xlNo
'¡ô³oÂX®i°Ï°ìÀx¦s®æ°µ¨â¼h¦¸ªº¾ã¦C±Æ§Ç,²Ó¸`¦p¤U:
'1.±N³oÂX®i°Ï°ìÀx¦s®æªº¬Û¹ï²Ä¤TÄæ¦ì(ªí¸Ìªº DÄæ),°µ¤p¨ì¤jªº±Æ§Ç
'2.¨Ã±N¤@¦¸±Æ§Ç«á¸Ì±¬Û¦PȪº¬Û¹ï²Ä¤KÄæ¦ì(ªí¸Ìªº IÄæ),°µ¤p¨ì¤jªº±Æ§Ç
End With
End If
Set Arr = Nothing: Set Y = Nothing: Set Z = Nothing
Erase V, T, Q
End Sub
Sub ²M°£_Á`ªí1()
Sheets("Á`ªí").UsedRange.Offset(5, 0).Offset(, 1).Delete Shift:=xlUp
'¡ô"Á`ªí"¤u§@ªí¦³¨Ï¥ÎªºÀx¦s®æ½d³ò,©¹¤U°¾²¾5¦C,¥ª¥k¤£°¾²¾,¤§«á¦A©¹¥k°¾²¾1Äæ,
'¦¹Àx¦s®æ½d³ò§R°£!¨Ã¥Ñ¤U¤èªºÀx¦s®æ©¹¤W»¼¸É
'Offset(5, 0).Offset(, 1) = Offset(5, 0).Offset(0 , 1)
'Offset(5, 0).Offset(, 1) = Offset(5).Offset( , 1)
'Offset(5, 0).Offset(, 1) = Offset(5, 1)
'Offset(5, ).Offset(, 1) ·|¥X²{ ½sĶ¿ù»~
'https://learn.microsoft.com/zh-tw/office/vba/api/excel.range.offset
End Sub |
|