您还没有绑定微信,更多功能请点击绑定

导入文本文件内容到单元格区域

Sub ImportRange() Dim ImpRng As Range Dim Filename As String Dim r As Long, c As Integer Dim txt As String, Char As String * 1 Dim Data Dim i As Integer Chr (34) Set ImpRng = ActiveCell On Error Resume Next Filename = Application.DefaultFilePath & "\textfile.csv" '定义文件路径 Open Filename For Input As #1 '打开文件 If Err <> 0 Then MsgBox "Not found: " & Filename, vbCritical, "ERROR" Exit Sub End If r = 0 c = 0 txt = "" Application.ScreenUpdating = False Do Until EOF(1) Line Input #1, Data For i = 1 To Len(Data) Char = Mid(Data, i, 1) If Char = "," Then ActiveCell.Offset(r, c) = txt c = c + 1 txt = "" '遇到逗号,偏移到下一单元格 ElseIf i = Len(Data) Then '每行结尾 If Char <> Chr(34) Then txt = txt & Char ActiveCell.Offset(r, c) = txt txt = "" ElseIf Char <> Chr(34) Then txt = txt & Char '正常的字符串就连在一起输出 End If Next i c = 0 r = r + 1 Loop Close #1 Application.ScreenUpdating = TrueEnd Sub

更多EXCEL精彩尽在:http://www.excelpx.com/?fromuid=148343

0 个评论

游客无法查看评论和回复, 请先登录注册

发起人

推荐文章

文章状态

  • 发布时间: 2011-01-27 21:29
  • 浏览: 1631
  • 评论: 0
  • 赞: 0