- ©«¤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-6-6 09:24 ½s¿è
¦^´_ 5# gaishutsusuru
ÁÂÁ«e½ú¦^´_
1.«á¾Ç¹ï©ó¤Óªøªº¤½¦¡¾Ç±o«ÜºC,½Ð«e½ú«Ý¼F®`ªº«e½úÀ°¦£
2.«á¾Ç¾ÇExcel¤Q¦h¦~»{¬°¤W³Â»¶®a±Ú½×¾Â¾Ç²ß¬O³Ì¦nªº³~®|,¾ÇExcel¥u¦³¨C¤Ñ½m²ß¨D¶i¨B,¨S¦³±¶®|,½×¾Â¦³«Ü¦h¾Ç¤£§¹ªº½d¨Ò,¨CÓ½d¨Ò¤S¥i¥H¥Î¦hºØ¤è®×¸Ñ¨M,¤£¥²¶R®Ñ,±`¤W³Â»¶®a±Ú½×¾Â¾Ç´N¹ï¤F
3.¥H¤U¬O½Æ²ß¬Q¤Ñªº¤è®×,µo²{«Ü¦h¦A§ï¶iªº,»P½Æ²ßªº¤ß±oµù¸Ñ,½Ð«e½ú°Ñ¦Ò,½Ð¦U¦ì«e½ú¤£§[«ü±Ð
Option Explicit'¤u§@ªí¼Ò²Õ
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'¡ô·í¤u§@ªí¤Wªº¿ï¨ú½d³òÅܧó®É·|µo¥Í¦¹¨Æ¥ó
With Target
'¡ô¥H¤U¬OÃö©óIJµoªºµ{§Ç
Dim T$, xR As Range
'¡ô«Å§iÅܼÆ
Set xR = Range([A2], [A65536].End(3))
'¡ô¥OxRÅܼƬO[A2]¨ìAÄæ³Ì«á¤@Ó¦³¤º®eÀx¦s®æ,³o½d³òÀx¦s®æ
If .Columns.Count <> 1 Or .Column <> 1 Then Exit Sub
'¡ô¦pªG¿ï¨úIJµoªºÄæ¼Æ¤£¬O1Äæ,©ÎÄ渹¤£¬O1,´Nµ²§ô°õ¦æ
If Intersect(xR, Selection.Cells) Is Nothing Then Exit Sub
'¡ô¦pªGxRÅܼƻP¿ï¨úIJµoªºÀx¦s®æ¨S¥æ¶°,´Nµ²§ô°õ¦æ
T = Intersect(xR, Selection.Cells).Address(0, 1)
'¡ô¥OTÅܼƬO xRÅܼƻP¿ï¨úIJµoªºÀx¦s®æ¥æ¶°Àx¦s®æ¦ì§}(Ä渹¦³$)
[H4] = T
'¡ô¥O[H4]Àx¦s®æȬO TÅܼÆ
End With
End Sub
Option Explicit'¤@¯ë¼Ò²Õ
Function SumText(xC As String, xY As String)
'¡ô¦Ûq¨ç¼ÆSumText(),«Å§iÅܼÆxC,xY³£¬O¦r¦êÅܼÆ
Dim Brr, Crr, V, Y, A, R&, i&, V1&, V2&, Z&, M&, j%, T$, Tr$
'¡ô«Å§iÅܼÆ(Brr,Crr,V,Y,A)¬O³q¥Î«¬ÅܼÆ,(R,i,V1,V2,Z,M)¬Oªø¾ã¼Æ,
'j¬Oµu¾ã¼Æ,(T,Tr)¬O¦r¦êÅܼÆ
Set Y = CreateObject("Scripting.Dictionary")
'¡ô¥OYÅܼƬO ¦r¨å
Brr = Range([D1], [A65536].End(3))
'¡ô¥OBrrÅܼƬO ¤Gºû°}¦C,¥HA~DÄæÀx¦s®æȱa¤J
For i = 2 To UBound(Brr)
'¡ô³]¶¶°j°é
For j = 3 To 4
'¡ô³]¶¶°j°é
Y(Brr(1, j)) = j
'¡ô¥O²Ä1¦Cj°j°éÄæBrr°}¦CÈ·íkey,item¬Oj°j°é¼Æ(°O¦~¤Àªº°}¦CÄ渹)
T = Brr(1, j) & "|" & Brr(i, 1): Tr = Brr(1, j) & "|" & i
'¡ô¥OTÅܼƬO ²Ä1¦Cj°j°éÄæBrr°}¦Cȳs±µ"|",¦A³s±µi°j°é²Ä1ÄæBrr°}¦CÈ,
'¥OTrÅܼƬO ²Ä1¦Cj°j°éÄæBrr°}¦Cȳs±µ"|",¦A³s±µi°j°é¼Æ
Y(T) = i: Y(T & "|Sum") = Y(T & "|Sum") + Brr(i, j)
'¡ô¥OTÅܼƬOkey,item¬OiÅܼÆ,¯Ç¤JY¦r¨å¤¤
'¡ô¥O¥HTÅܼƳs±µ"|Sum"²Õ¦¨·s¦r¦ê·íkey,item¬O
'¬O²Ö¥[i°j°é¦Cj°j°éÄæBrr°}¦CÈ
Y(Tr) = i: Y(Tr & "|Sum") = Y(Tr & "|Sum") + Brr(i, j)
'¡ô¥OTrÅܼƷíkey,item¬OiÅܼÆ,¯Ç¤JY¦r¨å¤¤,
'¡ô¥O¥HTrÅܼƳs±µ"|Sum"²Õ¦¨·s¦r¦ê·íkey,item¬O
'¬O²Ö¥[i°j°é¦Cj°j°éÄæBrr°}¦CÈ
Next
Next
T = Trim(xC): If T = "" Then GoTo 102
'¡ô¥OTÅܼƬO xCÅܼƥh°£«e«áªÅ¥Õ¦r¤¸«áªº·s¦r¦ê,
'¦pªGTÅܼƬO ªÅ¦r¤¸?´N¸õ¨ì¼Ð¥Ü102¦ì¸mÄ~Äò°õ¦æ
A = Split(Replace(Replace(xC, "$A", ""), ":", "~"), ",")
'¡ô¥OAÅܼƬOxCÅܼƦr¦ê³Q¤À³Î¦¨ªº¤@ºû°}¦C
'³QxCÅܼƳQ¤À³Î«e¥ý°µ2¦¸ªº¦r¤¸¸m´«,"$A"´«¦¨ "", ":"´«¦¨ "~"
'³Ì«á¥H³r¸¹¤À³Î¦¨¤@ºû°}¦C
If A(0) = "" Then Z = 0: GoTo 102
'¡ô¦pªG0¯Á¤Þ¸¹A°}¦CȬO ªÅ¦r¤¸?´N¥OZÅܼƬO0,¸õ¨ì¼Ð¥Ü102¦ì¸mÄ~Äò°õ¦æ
For Each V In A
'¡ô³]³v¶µ°j°é!¥OVÅܼƬO A°}¦C¸Ìªº¤@°}¦CÈ
V1 = Y(xY & "|" & Split(V, "~")(0))
'¡ô¥OV1ÅܼƬOxYÅܼƳs±µ"|",
'¦A³s±µ(VÅܼƥH"~"¤À³Î«áªº0¯Á¤Þ¸¹°}¦CÈ)©Ò²Õ¦¨ªº·s¦r¦ê¬d,
'¬dY¦r¨å¦^¶ÇitemÈ
V2 = Y(xY & "|" & StrReverse(Split(StrReverse(V), "~")(0)))
'¡ô¥O¥HV2ÅܼƬOxYÅܼƳs±µ"|",¦A³s±µ(VÅܼƦr¤¸¶¶§ÇÄAË«á,
'¥H"~"¤À³Î«áªº0¯Á¤Þ¸¹°}¦CÈ°µ¦r¤¸¶¶§ÇÄA˦^¨Ó,
'¥H¤W²Õ¦¨ªº·s¦r¦ê¬dY¦r¨å¦^¶ÇitemÈ
If InStr(V, "~") Then
'¡ô¦pªGVÅܼƸ̦³¥]§t"~" ?
If V1 * V2 = 0 Then Z = 0: GoTo 102
'¡ô¦pªGV1ÅܼƻPV2Åܼƪº¼¿n¬O 0,
'´N¥OZÅܼƬO0 , ¸õ¨ì¼Ð¥Ü102¦ì¸mÄ~Äò°õ¦æ
If V1 > V2 Then M = V2: V2 = V1: V1 = M
'¡ô¦pªGV1ÅܼƤj©óV2ÅܼÆ?´N¥OMÅܼƨó§UÅýV1.V2Ȥ¬´«
For R = V1 To V2: Z = Z + Brr(R, Y(xY)): Next
'¡ô³]¶¶°j°é!±qV1ÅܼƨìV2ÅܼÆ,¥OZÅܼƬO Brr°}¦CÈ,
'(R¬O«üBrr°}¦C¦C¸¹,Y(xY)¬O«ü$I$1Àx¦s®æȬdY¦r¨å°OªºÄ渹)
ElseIf Y(xY & "|" & V & "|Sum") = "" Then
'¡ô§_«h¦pªG¥H²Õ¦X¦r¦ê¬dY¦r¨å¸Ìitem¬OªÅ¦r¤¸?
Z = 0: GoTo 102
'´N¥OZÅܼƬO0 , ¸õ¨ì¼Ð¥Ü102¦ì¸mÄ~Äò°õ¦æ
Else
Z = Z + Y(xY & "|" & V & "|Sum")
'¡ô§_«h´N¥OZÅܼƲ֥[ ²Õ¦X¦r¦ê¬dY¦r¨å¸ÌitemÈ
End If
Next
102: If Z <> 0 Then SumText = Z Else SumText = ""
'¡ô¦pªGZÅܼƤ£¬O0,´N¥OSumText¨ç¼Æ¦^¶ÇZÅܼÆ(ªø¾ã¼ÆÅܼÆ),
'§_«h´N¦^¶ÇªÅ¦r¤¸
End Function |
|