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

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

¦^´_ 1# alexsas38
¥u­n¦b .Cells.ClearContents ¤U¤è¥[¤W :
.Columns(2).NumberFormat = "@" <- ¸Ñ¨M 0 ®ø¥¢ªº°ÝÃD(±NÀx¦s®æ®æ¦¡³]¬° "¤å¦r")

µM«á¦bµ{¦¡¶}ÀY¥[¤W :
Dim lRow As Long, lI As Long

±µµÛ¦b .Cells.QueryTable.Delete ¤U¤è¥[¤W :
lRow = .Cells(Rows.Count, 1).End(xlUp).Row
For lI = 1 To lRow
If Right(.Cells(lI, 1), 1) = "(" Then .Cells(lI, 2) = .Cells(lI, 1) & " " & .Cells(lI, 2) & " " & .Cells(lI, 3): .Cells(lI, 1) = "": .Cells(lI, 3) = ""
If .Cells(lI, 3) <> "" Then .Cells(lI, 2) = .Cells(lI, 2) & " " & .Cells(lI, 3) & " " & .Cells(lI, 4): .Cells(lI, 3) = "": .Cells(lI, 4) = ""
Next lI <- ¬JµMµ{¦¡·|©î¶}­ì¥ýªº¤å¦r ¨º»ò¦A¨Ì·Ó¨ä³W«h§â¸Ó¦X¨Öªº¦X¨Ö°_¨Ó´NOK¤F)
§Y¥i.
  1. Sub importtxt()
  2.     Dim path, folder, fname
  3.     Dim lRow As Long, lI As Long
  4.    
  5.     With Workbooks.Add '¼ÐÃD¦C

  6.         .Sheets(1).Range("A1:H1") = Array("Àç·~¤H²Î¤@½s¸¹", "­t³d¤H©m¦W", "Àç·~¤H¦WºÙ", "Àç·~¡]µ|Äy¡^µn°O¦a§}", "¸ê¥»ÃB(¤¸)", "²Õ´ºØÃþ", "³]¥ß¤é´Á", "µn°OÀç·~¶µ¥Ø")

  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 path = .SelectedItems(1) & "\"

  13.             End With

  14.             '¹ï©Ò¦³¸Ó¸ê®Æ§¨¤Uªºtxt³B²z

  15.             folder = Dir(path & "*.txt")

  16.             Do While folder <> ""

  17.                 .Cells.ClearContents
  18.                 .Columns(2).NumberFormat = "@"
  19.                
  20.                 '¶×¤J¥~³¡¸ê®Æ

  21.                 With .QueryTables.Add(Connection:="TEXT;" & path & folder, Destination:=.Range("A1"))

  22.                     .Name = "¸ê®Æ"
  23.                     .RefreshPeriod = 0

  24.                     .TextFileSpaceDelimiter = True  'ªÅ¥ÕÁ䬰¤À³Î¦r¤¸

  25.                     .Refresh BackgroundQuery:=False

  26.                 End With

  27.                 '§R°£¸ê®Æ³s½u

  28.                 .Cells.QueryTable.Delete
  29.                
  30.                 lRow = .Cells(Rows.Count, 1).End(xlUp).Row
  31.                 For lI = 1 To lRow
  32.                   If Right(.Cells(lI, 1), 1) = "(" Then .Cells(lI, 2) = .Cells(lI, 1) & " " & .Cells(lI, 2) & " " & .Cells(lI, 3): .Cells(lI, 1) = "": .Cells(lI, 3) = ""
  33.                   If .Cells(lI, 3) <> "" Then .Cells(lI, 2) = .Cells(lI, 2) & " " & .Cells(lI, 3) & " " & .Cells(lI, 4): .Cells(lI, 3) = "": .Cells(lI, 4) = ""
  34.                 Next lI

  35.                 '·s¼W¸ê®Æ¨ì²Ä¤@­Ó¤u§@ªí

  36.                 .Parent.Sheets(1).Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(, 8).Value = Application.Transpose(.Range("B1:B8").Value)

  37.                 folder = Dir

  38.             Loop

  39.             '§R°£¼È¦s¸ê®Æªí,¤£Åã¥Üĵ§iµøµ¡

  40.             Application.DisplayAlerts = False

  41.             .Delete

  42.             Application.DisplayAlerts = True

  43.         End With

  44.         .Sheets(1).Activate '¨Ï¶}±Ò¸ÓÀɮɪ½±µ¨ì²Ä¤@­Ó¤u§@ªí

  45.         '¦sÀÉ

  46.         fname = Application.GetSaveAsFilename(InitialFileName:=path & "final.xls", FileFilter:="Excel Files (*.xls),*.xls", Title:="Àx¦sÀÉ®×")

  47.         '°£«D«ö¨ú®ø, §_«h¦sÀÉ

  48.         If TypeName(fname) = "String" Then .SaveAs Filename:=fname, FileFormat:=xlExcel8

  49.     End With

  50. End Sub
½Æ»s¥N½X

TOP

        ÀR«ä¦Û¦b : ­n¥Î¤ß¡A¤£­n¾Þ¤ß¡B·Ð¤ß¡C
ªð¦^¦Cªí ¤W¤@¥DÃD