- ©«¤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
|
¦^´_ 38# aassddff736
ÁÂÁ½׾Â,ÁÂÁ«e½ú¤W½×¾Â¤@°_¾Ç²ß
«á¾Ç½Æ²ß¤F¤@¤U¨Ã§@µù¸Ñ,¶K¤W¨Ó½Ð«e½ú°Ñ¦Ò,½Ð¦U¦ì«e½ú«ü±Ð
Option Explicit
Private Sub CommandButton1_Click()
Dim Arr, Brr, Crr, C%, Z, N&, i&, j%, R&, Ta$, Tb$, xR As Range, xB As Range, xS As Worksheet
'¡ô«Å§iÅܼÆ,%¬Oµu¾ã¼Æ,&¬Oªø¾ã¼Æ,$¬O¦r¦êÅܼÆ,As Range¬OÀx¦s®æÅܼÆ,As Worksheet¬O¤u§@ªíÅܼÆ,¨ä¥¦¨S¦³«ü©wªº¬O³q¥Î«¬ÅܼÆ
Set Z = CreateObject("Scripting.Dictionary"): Set xS = Sheets("¤ñ¹ïµ²ªG")
'¡ô¥OZÅܼƬO¦r¨å,¥OxSÅܼƬO¤u§@ªí("¤ñ¹ïµ²ªG")
Set Arr = Sheets("¸ê®ÆA").[A1].CurrentRegion: Arr = Union(Arr, Arr.Offset(, 1))
'¡ô¥OArrÅܼƬO¤u§@ªí("¸ê®ÆA").[A1]Àx¦s®æ¬Û¾F¦êÁp«áÂX®i¦¨ªº³Ì¤p¤è¥¿°Ï°ìÀx¦s®æ
'¥OArrÅܼÆÅܨ¬°¤Gºû°}¦C (Áp¶°¦Û¨°Ï°ì©¹¥k°¾²¾1Äæ«áªº°Ï°ìÀx¦s®æȱa¤J°}¦C¤¤)
Set Brr = Sheets("¸ê®ÆB").[A1].CurrentRegion: Brr = Union(Brr, Brr.Offset(, 1))
'¡ô¥OBrrÅܼƬO¤u§@ªí("¸ê®ÆA").[A1]Àx¦s®æ¬Û¾F¦êÁp«áÂX®i¦¨ªº³Ì¤p¤è¥¿°Ï°ìÀx¦s®æ
'¥OBrrÅܼÆÅܨ¬°¤Gºû°}¦C (Áp¶°¦Û¨°Ï°ì©¹¥k°¾²¾1Äæ«áªº°Ï°ìÀx¦s®æȱa¤J°}¦C¤¤)
C = UBound(Arr, 2): If C <> UBound(Brr, 2) Then [B6:B8] = "": [B9] = 0: [B10] = 0: MsgBox "Äæ¼Æ¤£¦P": Exit Sub
'¡ô¥OCÅܼƬOArr°}¦C³Ì¤j¯Á¤ÞÄ渹
'¦pªGCÅܼƤ£¦P©óBrr°}¦C³Ì¤j¯Á¤ÞÄ渹!´N¥O[B6:B8]Àx¦s®æȬO ªÅ¦r¤¸:¥O[B9]¬O0,¥O[B10]¬O0,³Ì«á¸õ¥X´£µøµ¡~~~,µ²§ôµ{¦¡°õ¦æ
ReDim Crr(1 To (UBound(Arr) + UBound(Brr)), 1 To C * 2 + 1)
'¡ô«Å§iCrrÅܼƬO¤Gºû°}¦C,Áa¦V½d³ò±q1 ¨ì(Arr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹+Brr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹),¾î¦V½d³ò±q1 ¨ì(CÅܼÆ*2+1)
For i = 1 To UBound(Arr)
'¡ô³]¶¶°j°é!i±q1 ¨ìArr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹
Ta = Trim(Arr(i, 1)): R = R + 1: Z(Ta) = R: Crr(R, 1) = Ta
'¡ô¥OTa³o¦r¦êÅܼƬO i°j°é¦C1ÄæArr°}¦CÈ¥h°£ÀY§ÀªÅ¥Õ¦r¤¸«áªº·s¦r¦ê,¥ORÅܼƲ֥[1
'¥O¥HTaÅܼƬ°key,RÅܼƬ°item¯Ç¤JZ¦r¨å¤¤:¥ORÅܼƦC1ÄæCrr°}¦CȬO TaÅܼÆ
For j = 1 To C: Crr(R, j + 1) = Arr(i, j): Next
'¡ô³]¶¶°j°é!j±q1 ¨ìÅܼÆC: ¥ORÅܼƦC(j+1)ÄæCrr°}¦CȬO i°j°é¦Cj°j°éÄæArr°}¦CÈ
Next
For i = 1 To UBound(Brr)
'¡ô³]¶¶°j°é!i±q1 ¨ìBrr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹
Tb = Trim(Brr(i, 1)): N = Z(Tb): If N = 0 Then R = R + 1: Crr(R, 1) = Tb: N = R: Z(Tb) = R
'¡ô¥OTb³o¦r¦êÅܼƬO i°j°é¦C1ÄæBrr°}¦CÈ¥h°£ÀY§ÀªÅ¥Õ¦r¤¸«áªº·s¦r¦ê,¥ON³oªø¾ã¼ÆÅܼƬO¥HTbÅܼƬ°key¬dZ¦r¨å¦^¶ÇÈ(item)
'¦pªGNÅܼƬO 0,´N¥ORÅܼƲ֥[1:¥ORÅܼƦC1ÄæCrr°}¦CȬO TbÅܼÆ,¥ONÅܼÆ=RÅܼÆ,¥O¥HTbÅܼƬ°key,RÅܼƬ°item¯Ç¤JZ¦r¨å¤¤
For j = 1 To C: Crr(N, j + 1 + C) = Brr(i, j): Next
'¡ô³]¶¶°j°é!j±q1 ¨ìÅܼÆC: ¥ONÅܼƦC(j+1+C)ÄæCrr°}¦CȬO i°j°é¦Cj°j°éÄæBrr°}¦CÈ
Next
Application.Goto xS.[A1]
'¡ô¥O´å¼Ð¸õ¨ìxSÅܼƪº[A1]Àx¦s®æ¦ì¸m
xS.UsedRange.EntireRow.Delete: xS.[A1] = "NUMBER"
'¡ô¥OxSÅܼƸ̦³¨Ï¥ÎÀx¦s®æ©Ò¦bªº¦C§R°£,¥OxSÅܼƪº[A1]Àx¦s®æȬO¦r¦ê "NUMBER"
With xS.[A2].Resize(R, C * 2 + 1): .Value = Crr: .Sort KEY1:=.Item(1), Order1:=1, Header:=2: Crr = .Value: End With
'¡ô¥OxSÅܼƸÌ[A2]Àx¦s®æÂX®iè¦nªº°Ï°ì¥HCrr°}¦Cȱa¤J,¨Ã¥O¥H¸Ó°Ï°ìÀx¦s®æ¥H²Ä1Ä欰°ò·Ç°µ¨S¦³¼ÐÃD¦Cªº¶¶±Æ§Ç
xS.[B1] = [B2]: xS.[B1].Resize(, C - 1).Merge: xS.[B1].Interior.Color = [B2].Interior.Color
'¡ô¥OxSÅܼƸÌ[B1]Àx¦s®æȦP ¥»ªí[B2]Àx¦s®æÈ,¥O¼ÐÃD¦CÀx¦s®æ¦X¨Ö,¥O¸Ó¼ÐÃD¦CÀx¦s®æ©³¦â¦P ¥»ªí[B2]Àx¦s®æ©³¦â
xS.[B1].Item(, C + 1) = [B3]: xS.[B1].Item(, C + 1).Resize(, C - 1).Merge: xS.[B1].Item(, C + 1).Interior.Color = [B3].Interior.Color
'¡ô¥OxSÅܼƸÌ[B1]¦Û¨¶}©l©¹¥kC+1ÄæÀx¦s®æȦP ¥»ªí[B3]Àx¦s®æÈ
'¥O¼ÐÃD¦CÀx¦s®æ¦X¨Ö,¥O¸Ó¼ÐÃD¦CÀx¦s®æ©³¦â¦P ¥»ªí[B3]Àx¦s®æ©³¦â
xS.UsedRange.EntireColumn.AutoFit: Set xR = xS.UsedRange: Set xR = xR(xR.Count + 1): Set xB = xR: xS.[1:1].HorizontalAlignment = xlCenter
'¡ô¥OxSÅܼƸ̦³¨Ï¥ÎÀx¦s®æ©Ò¦bÄæ¦ì¦Û°Ê½Õ¾ãÄæ¼e,¥OxRÅܼƬO xSÅܼƸ̦³¨Ï¥ÎÀx¦s®æ: ¥OxRÅܼƬO¨S¨Ï¥Îªº¦a1ÓÀx¦s®æ
'¥OxBÅܼƬO¦PxRÅܼÆ(Àx¦s®æ):¥OxSÅܼƸ̲Ä1¦CÀx¦s®æ®æ¦¡ ¤ô¥¤å¦r¸m¤¤
For i = 2 To R + 1
'¡ô³]¶¶°j°é!i±q2 ¨ìRÅܼÆ+1
For j = 2 To C
'¡ô³]¶¶°j°é!j±q2 ¨ìCÅܼÆ
Set xR = IIf(Crr(i - 1, j) <> Crr(i - 1, j + C), Union(xR, xS.Cells(i, j), xS.Cells(i, 1)), xR)
'¡ô¥O¨â°Ï°ì¬Û¹ïÄæ¦ìÀx¦s®æȦpªG¤£¦P!´N±N¥ª°ÏÀx¦s®æ»PAÄæÀx¦s®æ ¯Ç¤JxRÅܼƤ¤
If Crr(i - 1, j) = "" Or Crr(i - 1, j + C) = "" Then Set xB = Union(xB, xS.Cells(i, j))
'¡ô¥O¨â°Ï°ì¬Û¹ïÄæ¦ìÀx¦s®æȦpªG¬OªÅ®æ!´N±N¥ª°ÏÀx¦s®æ ¯Ç¤JxBÅܼƤ¤
Next
Next
Union(xR, xR.Offset(, C)).Font.ColorIndex = 3
'¡ô¥O¨â°Ï°ì¤ñ¹ï¥X¤£¦PªºÀx¦s®æ ³s¦PAÄæÀx¦s®æxRÅܼƦr¦âÅܬ°¬õ¦â
xB.EntireRow.Font.ColorIndex = 5
'¡ô¥OxBÅܼƩҦb¦C¾ã¦C¦r¦âÅܬ°ÂŦâ
With Sheets("¯d¤U¬Û¦P")
'¡ô¥H¤U¬OÃö©ó¤u§@ªí("¯d¤U¬Û¦P")ªºµ{§Ç
.UsedRange.EntireRow.Delete: xS.UsedRange.Copy .[A1]: .UsedRange.EntireColumn.AutoFit
'¡ô¥O¸Óªí¦³¨Ï¥ÎÀx¦s®æ©Ò¦bªº¾ã¦C§R°£: ¥OxSÅܼƦ³¨Ï¥ÎÀx¦s®æ½Æ»s¨ì¸Óªí[A1]: ¥O¸Óªí¦³¨Ï¥ÎÀx¦s®æ©Ò¦bÄæ¦Û°Ê½Õ¾ãÄæ¼e
.Range(Intersect(xR.EntireRow, xS.UsedRange).Address).EntireRow.Delete
'¡ô¥O¦³®t²§ªº¦C¾ã¦C§R°£
End With
With Sheets("¯d¤U®t²§")
'¡ô¥H¤U¬OÃö©ó¤u§@ªí("¯d¤U®t²§")ªºµ{§Ç
.UsedRange.EntireRow.Delete: Intersect(Union(xS.[A1], xR.EntireRow), xS.UsedRange).EntireRow.Copy .[A1]: .UsedRange.EntireColumn.AutoFit
'¡ô¥O¸Óªí¦³¨Ï¥ÎÀx¦s®æ©Ò¦bªº¾ã¦C§R°£: ¥OxSÅܼƦ³®t²§ªº¦C¾ã¦C½Æ»s¨ì¸Óªí[A1]: ¥O¸Óªí¦³¨Ï¥ÎÀx¦s®æ©Ò¦bÄæ¦Û°Ê½Õ¾ãÄæ¼e
End With
[B6] = C - 1: [B7] = UBound(Arr): [B8] = UBound(Brr): [B9] = 1: [B10] = 1
'¡ô¥O¥»ªí[B6]ȬO CÅܼÆ-1: ¥O[B7]ȬOArr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹: ¥O[B8]ȬOBrr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹
End Sub |
|