Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 13153|回复: 6

[已解决]菜鸟求一个VBA,将一列的各个单元格内容导出为txt文件

[复制链接]
发表于 2013-3-7 10:49 | 显示全部楼层 |阅读模式
求助的vba程序说明如下 1,某一列的各个单元格如A1, A2, A3, ......, 分别存放了不同的内容,我想把每一个单元格的内容导出为一个单独的txt文件,自动命名为1.txt, 2.txt, 3.txt, ......, 以此类推。
2,如果遇到某个单元格内容为空,则工作完成。
3,导出完成后,跳出对话框提示。
最佳答案
2013-3-7 11:04
  1. Sub txt()
  2. '---------------------------------------------------------------------------------------
  3. ' Procedure : txt
  4. ' Author    : hwc2ycy
  5. ' Date      : 2013/3/7
  6. ' Purpose   :
  7. '---------------------------------------------------------------------------------------
  8. '
  9. '数据行
  10.     Dim lRow As Long
  11.     'TXT文件编号
  12.     Dim lFile As Long
  13.     '文件号
  14.     Dim iFn As Byte
  15.     '保存位置
  16.     Dim sPath As String
  17.     '循环计数
  18.     Dim i As Long
  19.     'A列数组
  20.     Dim arr
  21.     '错误消息
  22.     Dim strError As String

  23.     sPath = ThisWorkbook.Path & Application.PathSeparator
  24.     lRow = Cells(Rows.Count, 1).End(xlUp).Row
  25.     arr = Range("a1:b" & lRow)

  26.     On Error Resume Next
  27.     For i = 1 To lRow
  28.         If Len(arr(i, 1)) > 0 Then
  29.             lFile = lFile + 1
  30.             '取文件号
  31.             iFn = FreeFile
  32.             '创建文件,每次内容均被覆盖,如果要追加就把OUTPUT改成APPEND
  33.             Open sPath & lFile & ".txt" For Output As #iFn
  34.             If Err.Number <> 0 Then
  35.                 strError = strError & "A" & i & "写入TXT失败"
  36.                 Err.Clear
  37.             Else
  38.                 '写入A列对应行的数据
  39.                 Print #iFn, arr(i, 1)
  40.                 '关闭文件
  41.                 Close #iFn
  42.             End If
  43.         End If
  44.     Next
  45.    
  46.     '判断是否有写入失败的单元格
  47.     If Len(strError) > 0 Then
  48.         MsgBox strError
  49.     Else
  50.         MsgBox "输出完成"
  51.     End If
  52. End Sub
复制代码
发表于 2013-3-7 10:57 | 显示全部楼层
  1. Sub txt()
  2.     Dim lRow As Long
  3.     Dim lFile As Long
  4.     Dim iFn As Integer
  5.     Dim sPath As String
  6.     Dim i As Long
  7.    
  8.     Dim arr
  9.     sPath = ThisWorkbook.Path & Application.PathSeparator
  10.     lRow = Cells(Rows.Count, 1).End(xlUp).Row
  11.     arr = Range("a1:b" & lRow)
  12.     For i = 1 To lRow
  13.         If Len(arr(i, 1)) > 0 Then
  14.             lFile = lFile + 1
  15.             iFn = FreeFile
  16.             Open sPath & lFile & ".txt" For Output As #iFn
  17.             Print #iFn, arr(i, 1)
  18.             Close #iFn
  19.         End If
  20.     Next
  21.     MsgBox "输出完成"
  22. End Sub
复制代码
回复

使用道具 举报

发表于 2013-3-7 11:04 | 显示全部楼层    本楼为最佳答案   
  1. Sub txt()
  2. '---------------------------------------------------------------------------------------
  3. ' Procedure : txt
  4. ' Author    : hwc2ycy
  5. ' Date      : 2013/3/7
  6. ' Purpose   :
  7. '---------------------------------------------------------------------------------------
  8. '
  9. '数据行
  10.     Dim lRow As Long
  11.     'TXT文件编号
  12.     Dim lFile As Long
  13.     '文件号
  14.     Dim iFn As Byte
  15.     '保存位置
  16.     Dim sPath As String
  17.     '循环计数
  18.     Dim i As Long
  19.     'A列数组
  20.     Dim arr
  21.     '错误消息
  22.     Dim strError As String

  23.     sPath = ThisWorkbook.Path & Application.PathSeparator
  24.     lRow = Cells(Rows.Count, 1).End(xlUp).Row
  25.     arr = Range("a1:b" & lRow)

  26.     On Error Resume Next
  27.     For i = 1 To lRow
  28.         If Len(arr(i, 1)) > 0 Then
  29.             lFile = lFile + 1
  30.             '取文件号
  31.             iFn = FreeFile
  32.             '创建文件,每次内容均被覆盖,如果要追加就把OUTPUT改成APPEND
  33.             Open sPath & lFile & ".txt" For Output As #iFn
  34.             If Err.Number <> 0 Then
  35.                 strError = strError & "A" & i & "写入TXT失败"
  36.                 Err.Clear
  37.             Else
  38.                 '写入A列对应行的数据
  39.                 Print #iFn, arr(i, 1)
  40.                 '关闭文件
  41.                 Close #iFn
  42.             End If
  43.         End If
  44.     Next
  45.    
  46.     '判断是否有写入失败的单元格
  47.     If Len(strError) > 0 Then
  48.         MsgBox strError
  49.     Else
  50.         MsgBox "输出完成"
  51.     End If
  52. End Sub
复制代码
回复

使用道具 举报

发表于 2013-3-7 11:09 | 显示全部楼层
A列写入TXT.rar (17.11 KB, 下载次数: 307)
回复

使用道具 举报

 楼主| 发表于 2013-3-7 11:26 | 显示全部楼层
很好用。谢谢hwc2ycy.

点评

帮你解决一个问题,我就学到一个知识点。  发表于 2013-3-7 11:28
回复

使用道具 举报

匿名  发表于 2014-10-10 12:40
hwc2ycy 发表于 2013-3-7 11:09

感谢你的帮助,挺好用的,呵呵
回复

使用道具

匿名  发表于 2014-11-14 10:08
hwc2ycy 发表于 2013-3-7 11:09

表格非常好用。多谢高手分享
回复

使用道具

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-25 07:43 , Processed in 0.537865 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表