返回列表 上一主題 發帖

以ado方式,將csv檔放進陣列中出錯?(當中有英文字母)

回復 2# joey0415
提供你參考:
  1. Sub CSV_Import()
  2.     Dim ws As Worksheet, strFile As String

  3.     Sheets("檔案名稱").Select
  4.     Cells.Clear
  5.    
  6.     Set ws = ActiveSheet 'set to current worksheet name
  7.     '  strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
  8.     '  你也可以直接指定檔案名稱
  9.     strFile = ThisWorkbook.Path & "\9962.csv"

  10.     With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
  11.          .TextFileParseType = xlDelimited
  12.          .TextFileCommaDelimiter = True
  13.          .Refresh
  14.     End With
  15. End Sub
複製代碼

TOP

回復 1# joey0415
  1. Sub CSV_Import()
  2.     Dim ws As Worksheet, strFile As String, i As Long
  3.     Dim arr
  4.    
  5.     Sheets("檔案名稱").Select
  6.     Cells.Clear
  7.    
  8.     Set ws = ActiveSheet 'set to current worksheet name
  9.     '  strFile = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
  10.     '  你也可以直接指定檔案名稱
  11.     strFile = ThisWorkbook.Path & "\9962.csv"

  12.     With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
  13.          .TextFileParseType = xlDelimited
  14.          .TextFileCommaDelimiter = True
  15.          .Refresh
  16.     End With
  17.    
  18.     arr = Range("A1:E" & Range("A" & Rows.Count).End(xlUp).Row)
  19.     For i = LBound(arr, 2) To UBound(arr, 1)
  20.         Debug.Print arr(i, 1) & ","; arr(i, 2) & ", " & arr(i, 3) & ", " & arr(i, 4) & ", " & arr(i, 5) & ";"
  21.     Next i
  22. End Sub
複製代碼

TOP

        靜思自在 : 修行要繫緣修心,藉事練心,隨處養心。
返回列表 上一主題