- ©«¤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½ú
¥H¤U¤ß±oµù¸Ñ,½Ð¦U¦ì«e½ú«ü±Ð
Option Explicit
Sub «ö¶s1_Click()
Application.DisplayAlerts = False
'¡ô¥O°õ¦æ¨ì¬O§_Àx¦s¨ú¥NÂÂÀÉ®É,¤£¸õ¥X¸ß°Ýµ¡,ª½±µ¨ú¥NÀx¦s
Dim MyFile, Arr, Q, Brr$(), R&, Sc%, p2%, MDS$(2 To 4), mcs1$, mcs2$, Nm$, myPath$
'¡ô«Å§iÅܼÆ:(MyFile,Arr,Q)¬O³q¥Î«¬ÅܼÆ,Brr¬O°}¦C(°}¦CȬ°¦r¦ê),R¬Oªø¾ã¼Æ,
'(Sc,p2)¬Oµu¾ã¼Æ,MDS¬O¤@ºû°}¦C(¯Á¤Þ¸¹2~4),(mcs1, mcs2,Nm,myPath)¬O¦r¦êÅܼÆ
MyFile = Application.GetOpenFilename("ExcelÀÉ,*.XLS*")
'¡ô¥OMyFile³o³q¥Î«¬ÅܼƬO Åã¥Ü¼Ð·Çªº [¶}±ÒÂÂÀÉ] ¹ï¸Ü¤è¶ô¡A±q¨Ï¥ÎªÌ¨ú±oÀɮתº¦WºÙ
https://learn.microsoft.com/zh-t ... ion.getopenfilename
If MyFile = False Then Exit Sub
'¡ô¦pªGMyFileÅܼƬOÅÞ¿èÈ False,¥Nªí¨S¦³¿ï¨úÀÉ®×,µ²§ôµ{§Ç°õ¦æ
Q = Split([B6], ",")
'¡ô¥OQ³o³q¥Î«¬ÅܼƬO[B6]Àx¦s®æ¥H ³r¸¹¤À³Î¦¨ªº¤@ºû°}¦C
myPath = ThisWorkbook.Path & "\"
'¡ô¥OmyPath³o¦r¦êÅܼƬO ¥»¬¡¶Ã¯©Ò¦bªº¸ô®|³s±µ "\"©Ò²Õ¦¨ªº¦r¦ê
mcs1 = [B1] & Mid([B2], 2, 6) & [B3] & [B4]
'¡ô¥Omcs1³o¦r¦êÅܼƬO [B1]Àx¦s®æÈ,³s±µ[B2]Àx¦s®æȨú²Ä2¦r¤¸¶}©lªº6¦r¤¸¦r¦ê,
'¦A³s±µ[B3]Àx¦s®æÈ,³Ì«á³s±µ[B4]Àx¦s®æÈ,²Õ¦X¦¨ªº¦r¦ê
mcs2 = [B5]: Sc = Val([B7])
'¡ô¥Omcs2³o¦r¦êÅܼƬO [B5]Àx¦s®æȦr¦ê
'¥OSc³oµu¾ã¼Æ¬O [B7]Àx¦s®æÈÂà¤Æªº¾ã¼ÆÈ
If Sc <= 0 Then MsgBox "[B7]Àx¦s®æ¿é¤J¿ù»~": Exit Sub
'¡ô¦pªGScÅܼƤp©ó©Îµ¥©ó0!,´N¸õ¥X´£¥Üµ¡~~~,µ²§ôµ{§Ç°õ¦æ
With Workbooks.Open(MyFile, , True)
'¡ô¥H¤U¬OÃö©ó µL±K½X/°ßŪ/¶}±ÒMyFileÅÜ¼Æ ÂÂÀɪº¬ÛÃöµ{§Ç
Nm = .Name
'¡ô¥ONm³o¦r¦êÅܼƬO ³o¶}±ÒÀɮצW¦r¦r¦ê
If Sc > .Worksheets.Count Then MsgBox Nm & " ¬¡¶Ã¯¨S¦³²Ä" & Sc & " Óªí": .Close 0: Exit Sub
'¡ô¦pªGScÅܼƤj©ó¦¹ÂÂÀɤu§@ªí¼Æ¶q!´N¸õ¥X´£µøµ¡~~~,Ãö³¬ÀÉ®×(¤£Àx¦s),µ²§ôµ{§Ç°õ¦æ
With .Sheets(Sc)
'¡ô¥H¤U¬OÃö©ó¦¹ÂÂÀɪº²Ä ScÅܼÆÓ¤u§@ªíªº¬ÛÃöµ{§Ç
If Not .AutoFilter Is Nothing Then If .FilterMode = True Then .ShowAllData
'¡ô¦pªG¦³¿z¿ï¥\¯à!´N§PÂ_¦pªG¬O¿z¿ïª¬ºA!¥O²M°£¿z¿ï
Arr = Range(.[G1], .[A65536].End(3))
'¡ô¥OArr³o³q¥Î«¬ÅܼƬO ¤Gºû°}¦C,¥H¸Óªíªº[G2]¨ìAÄæ³Ì«á¦³¤º®eªºÀx¦s®æ,
'¦¹½d³òÀx¦s®æȱa¤J°}¦C¤¤
End With
.Close 0
'¡ô¥O¸ÓÂÂÀÉÃö³¬(¤£Àx¦s)
End With
If UBound(Arr) = 1 Then MsgBox Nm & " ¬¡¶Ã¯²Ä" & Sc & " Óªí¨S¦³¸ê®Æ": Exit Sub
'¡ô¦pªGArr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹¬O 1!´N¸õ¥X´£µøµ¡~~~,µ²§ôµ{§Ç°õ¦æ
ReDim Brr(1 To UBound(Arr) - 1, 1 To 1)
'¡ô«Å§iBrr°}¦CÁa¦V½d³ò±q¯Á¤Þ¸¹1 ¨ìArr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹-1,¾î¦V½d³ò±q1~1¯Á¤Þ¸¹
For R = 2 To UBound(Arr)
'¡ô³]¶¶°j°é!¥ORÅܼƱq2 ¨ìArr°}¦CÁa¦V³Ì¤j¯Á¤Þ¦C¸¹
If Arr(R, 3) <> "" Then
'¡ô¦pªGR°j°é¦C²Ä3ÄæArr°}¦CȤ£¬OªÅ¦r¤¸?
p2 = 29 - Len(Arr(R, Val(Q(2))))
'¡ô¥Op2³oµu¾ã¼ÆÅܼƬO 29-(°j°é°}¦CȪº¦r¤¸¼Æ)
'°j°é°}¦CÈ:(R°j°é¦C,2¯Á¤Þ¸¹Q°}¦CÈÂର¼ÆȪºÄ渹)Arr°}¦CÈ
MDS(2) = Arr(R, Val(Q(0)))
'¡ô¥O2¯Á¤Þ¸¹MDS°}¦CȬO (R°j°é¯Á¤Þ¦C,0¯Á¤Þ¸¹Q°}¦CÈÂର¼ÆȪº¯Á¤ÞÄ渹)Arr°}¦CÈ
MDS(3) = Format(Arr(R, Val(Q(1))), "00000000000")
'¡ô¥O3¯Á¤Þ¸¹MDS°}¦CȬO (R°j°é¯Á¤Þ¦C,1¯Á¤Þ¸¹Q°}¦CÈÂର¼ÆȪº¯Á¤ÞÄ渹)Arr°}¦CÈ,
'¦A±N¦¹ÈÂà¤Æ¬° "00000000000"®æ¦¡ªº¦r¦ê
MDS(4) = Arr(R, Val(Q(2)))
'¡ô¥O4¯Á¤Þ¸¹MDS°}¦CȬO (R°j°é¯Á¤Þ¦C,2¯Á¤Þ¸¹Q°}¦CÈÂର¼ÆȪº¯Á¤ÞÄ渹)Arr°}¦CÈ
Brr(R - 1, 1) = mcs1 & MDS(2) & MDS(3) & "00" & mcs2 & MDS(4) & String(p2, " ")
'¡ô¥O(R°j°é-1)¦C1ÄæBrr°}¦CȬO ±µÄò¦hÓ¦r¦ê²Õ¦¨ªº¦r¦ê
'String(p2, " "):¬Op2Ӫťզr¤¸ªº¦r¦ê
End If
Next R
Workbooks.Add
'¡ô¥O·s¼W¤@Ó¬¡¶Ã¯
[A1].Resize(UBound(Arr) - 1, 1) = Brr
'¡ô¥O[A1]Àx¦s®æÂX®i¦V¤U(Arr°}¦CÁa¦V³Ì¤j¯Á¤Þ¸¹-1)¦Cªº½d³òÀx¦s®æÈ¥HBrr°}¦Cȱa¤J
Nm = StrReverse(Mid(StrReverse(Nm), InStr(StrReverse(Nm), ".") + 1))
'¡ô¥ONmÅܼƦr¦ê¤ÏÂà«á ¨ú"."«á(¤£§t)ªº©Ò¦³¦r¤¸,¦A¤ÏÂà¦^¨Ó
ActiveWorkbook.SaveAs myPath & Nm & ".TXT", 42
'¡ô¥OÀx¦s¬° ¤å¦r¦rÀÉ(Unicode ¤å¦r)
https://learn.microsoft.com/zh-t ... /excel.xlfileformat
ActiveWindow.Close
'¡ô¥O¦¹µøµ¡¬¡¶Ã¯ Ãö³¬
ThisWorkbook.Activate
'¡ô¿E¬¡¥»ÀÉ
MsgBox "²£¥Í´CÅéÀÉ:" & myPath & Nm & ".TXT"
'¡ô¸õ¥X´£¥Üµ¡ ~~~
End Sub |
|