|
求助的vba程序说明如下 1,某一列的各个单元格如A1, A2, A3, ......, 分别存放了不同的内容,我想把每一个单元格的内容导出为一个单独的txt文件,自动命名为1.txt, 2.txt, 3.txt, ......, 以此类推。
2,如果遇到某个单元格内容为空,则工作完成。
3,导出完成后,跳出对话框提示。
- Sub txt()
- '---------------------------------------------------------------------------------------
- ' Procedure : txt
- ' Author : hwc2ycy
- ' Date : 2013/3/7
- ' Purpose :
- '---------------------------------------------------------------------------------------
- '
- '数据行
- Dim lRow As Long
- 'TXT文件编号
- Dim lFile As Long
- '文件号
- Dim iFn As Byte
- '保存位置
- Dim sPath As String
- '循环计数
- Dim i As Long
- 'A列数组
- Dim arr
- '错误消息
- Dim strError As String
- sPath = ThisWorkbook.Path & Application.PathSeparator
- lRow = Cells(Rows.Count, 1).End(xlUp).Row
- arr = Range("a1:b" & lRow)
- On Error Resume Next
- For i = 1 To lRow
- If Len(arr(i, 1)) > 0 Then
- lFile = lFile + 1
- '取文件号
- iFn = FreeFile
- '创建文件,每次内容均被覆盖,如果要追加就把OUTPUT改成APPEND
- Open sPath & lFile & ".txt" For Output As #iFn
- If Err.Number <> 0 Then
- strError = strError & "A" & i & "写入TXT失败"
- Err.Clear
- Else
- '写入A列对应行的数据
- Print #iFn, arr(i, 1)
- '关闭文件
- Close #iFn
- End If
- End If
- Next
-
- '判断是否有写入失败的单元格
- If Len(strError) > 0 Then
- MsgBox strError
- Else
- MsgBox "输出完成"
- End If
- End Sub
复制代码
|
|