ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

Excel 2007 VBAŪTXTÀɨÃÂà¸m

¦^´_ 1# alexsas38
  1. Sub TEST()
  2.     Dim fd, f, fo
  3.    
  4.     With Workbooks.Add
  5.         '¼ÐÃD¦C
  6.         .Sheets(1).Range("A1:D1") = Array("¦W¦r", "¼Æ¾Ç", "­^¤å", "¦a²z")
  7.         '·s¼W¼È¦s¸ê®Æªí
  8.         With .Sheets.Add(after:=.Sheets(.Sheets.Count))
  9.             'ÂsÄý¿ï¾Ü¸ê®Æ§¨
  10.             With Application.FileDialog(msoFileDialogFolderPicker)
  11.                 .AllowMultiSelect = False
  12.                 If .Show = -1 Then fd = .SelectedItems(1) & "\"
  13.             End With
  14.             '¹ï©Ò¦³¸Ó¸ê®Æ§¨¤Uªºtxt³B²z
  15.             f = Dir(fd & "*.txt")
  16.             Do While f <> ""
  17.                 .Cells.ClearContents
  18.                 '¶×¤J¥~³¡¸ê®Æ
  19.                 With .QueryTables.Add(Connection:="TEXT;" & fd & f, Destination:=.Range("A1"))
  20.                     .Name = "¦¨ÁZ"
  21.                     .RefreshPeriod = 0
  22.                     .TextFileParseType = xlDelimited
  23.                     .TextFileConsecutiveDelimiter = True
  24.                     .TextFileTabDelimiter = True    'TabÁ䬰¤À³Î¦r¤¸
  25.                     .TextFileSemicolonDelimiter = False
  26.                     .TextFileCommaDelimiter = False
  27.                     .TextFileSpaceDelimiter = True  'ªÅ¥ÕÁ䬰¤À³Î¦r¤¸
  28.                     .Refresh BackgroundQuery:=False
  29.                 End With
  30.                 '§R°£¸ê®Æ³s½u
  31.                 .Cells.QueryTable.Delete
  32.                 '·s¼W¸ê®Æ¨ì²Ä¤@­Ó¤u§@ªí
  33.                 .Parent.Sheets(1).Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 4).Value = Application.Transpose(.Range("B1:B4").Value)
  34.                 f = Dir
  35.             Loop
  36.             '§R°£¼È¦s¸ê®Æªí,¤£Åã¥Üĵ§iµøµ¡
  37.             Application.DisplayAlerts = False
  38.             .Delete
  39.             Application.DisplayAlerts = True
  40.         End With
  41.         .Sheets(1).Activate '¨Ï¶}±Ò¸ÓÀɮɪ½±µ¨ì²Ä¤@­Ó¤u§@ªí
  42.         '¦sÀÉ
  43.         fo = Application.GetSaveAsFilename(InitialFileName:=fd & "final.xls", FileFilter:="Excel Files (*.xls),*.xls", Title:="Àx¦sÀÉ®×")
  44.         '°£«D«ö¨ú®ø, §_«h¦sÀÉ
  45.         If TypeName(fo) = "String" Then .SaveAs Filename:=fo, FileFormat:=xlExcel8
  46.     End With
  47. End Sub
½Æ»s¥N½X

TOP

¦^´_ 4# alexsas38
³o¼ËÀÉ®×¥u¯à¦Û¤v¥ÎSplit­åªR¡G
  1. Sub TEST()
  2.     Dim fd, f, fo
  3.     Dim ar(), fnum As Integer, i, s
  4.     Dim arData() As String, dataLine As String
  5.    
  6.     ReDim ar(0)
  7.     ar(0) = Array("Àç·~¤H²Î¤@½s¸¹", "­t³d¤H©m¦W", "Àç·~¤H¦WºÙ", "Àç·~¡]µ|Äy¡^µn°O¦a§}", "¸ê¥»ÃB(¤¸)", "²Õ´ºØÃþ", "³]¥ß¤é´Á", "µn°OÀç·~¶µ¥Ø")
  8.    
  9.     With Workbooks.Add
  10.         'ÂsÄý¿ï¾Ü¸ê®Æ§¨
  11.         With Application.FileDialog(msoFileDialogFolderPicker)
  12.             If .Show = -1 Then
  13.                 If .SelectedItems.Count > 0 Then fd = .SelectedItems(1) & "\"
  14.             Else
  15.                 Exit Sub    '¨ú®ø
  16.             End If
  17.         End With
  18.         '¹ï©Ò¦³¸Ó¸ê®Æ§¨¤Uªºtxt³B²z
  19.         f = Dir(fd & "*.txt")
  20.         Do While f <> ""
  21.             'Ū¨úÀÉ®×
  22.             fnum = FreeFile
  23.             Open fd & f For Input As #fnum
  24.             '¥ÎSplit­åªR«e¤K¦æ¸ê®Æ
  25.             ReDim arData(0 To 7)
  26.             For i = 0 To 7
  27.                 If EOF(fnum) Then Exit For  '­YÀÉ®×¥¼¹F¤K¦æ«h¸õ¥X
  28.                 Line Input #fnum, dataLine
  29.                 s = Split(dataLine, " ", 2)     '­­¨î³Ì¦h¶Ç¦^ªº¤l¦r¦ê¼Æ¬°2­Ó
  30.                 If UBound(s) = 1 Then arData(i) = s(1)
  31.             Next
  32.             ReDim Preserve ar(UBound(ar) + 1)   '«O¯d¨Ã¼W¤j°}¦C
  33.             ar(UBound(ar)) = arData
  34.             Close #fnum     '°O±oÃöÀÉ®×
  35.             f = Dir
  36.         Loop
  37.         With .Sheets(1)
  38.             .Columns("A:A").NumberFormatLocal = "@"     'AÄæ®æ¦¡³]¬°¤å¦r
  39.             .Range("A1").Resize(UBound(ar) + 1, 8).Value = Application.Transpose(Application.Transpose(ar))   '¶ñ¤J¸ê®Æ
  40.             .Range("A1").Resize(UBound(ar) + 1, 8).EntireColumn.AutoFit   '½Õ¾ãÄæ¼e
  41.         End With
  42.         '¦sÀÉ
  43.         fo = Application.GetSaveAsFilename(InitialFileName:=fd & "final.xls", FileFilter:="Excel Files (*.xls),*.xls", Title:="Àx¦sÀÉ®×")
  44.         '°£«D«ö¨ú®ø, §_«h¦sÀÉ
  45.         If TypeName(fo) = "String" Then .SaveAs Filename:=fo, FileFormat:=xlExcel8
  46.     End With
  47. End Sub
½Æ»s¥N½X

TOP

        ÀR«ä¦Û¦b : ¤@­Ó¤H¤£©È¿ù¡A´N©È¤£§ï¹L¡A§ï¹L¨Ã¤£Ãø¡C
ªð¦^¦Cªí ¤W¤@¥DÃD