VBA代码将Excel工作表另存为制表符分隔的文本文件 [英] VBA code to save Excel sheet as tab-delimited text file

查看:172
本文介绍了VBA代码将Excel工作表另存为制表符分隔的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这段代码并尝试了它.它工作正常,但是在已保存的文本文件中,第一列具有空格(用于所有行).我无法修复此代码.

I found this code and tried it. It works fine, but in the saved text file the first column has blank space (for all rows). I am unable to fix this code.

Sub ExportRange()
Dim ExpRng As Range
Open ThisWorkbook.Path & "\AllDXL.txt" For Output As #1
Set ExpRng = Worksheets("Sheet1").Range("A1").CurrentRegion
FirstCol = ExpRng.Columns(1).Column
LastCol = FirstCol + ExpRng.Columns.Count - 1
FirstRow = ExpRng.Rows(1).Row
LastRow = FirstRow + ExpRng.Rows.Count - 1
For r = FirstRow To LastRow
 Data = ""
 For c = FirstCol To LastCol
  '  data = ExpRng.Cells(r, c).Value
  Data = Data & vbTab & ExpRng.Cells(r, c).Value
Next c
Print #1, Data
Next r
Close #1
End Sub

推荐答案

这也可行...

Sub ExportRange()
Dim ExpRng As Range
Dim myTab As String
Open ThisWorkbook.Path & "\AllDXL.txt" For Output As #1
    Set ExpRng = Worksheets("Sheet1").Range("A1").CurrentRegion
    FirstCol = ExpRng.Columns(1).Column
    LastCol = FirstCol + ExpRng.Columns.Count - 1
    FirstRow = ExpRng.Rows(1).Row
    LastRow = FirstRow + ExpRng.Rows.Count - 1
    For r = FirstRow To LastRow
       Data = ""
       For c = FirstCol To LastCol
           If c = 1 Then myTab = "" Else myTab = vbTab
        '  data = ExpRng.Cells(r, c).Value
           Data = Data & myTab & ExpRng.Cells(r, c).Value
       Next c
       Print #1, Data
   Next r
Close #1
End Sub

这篇关于VBA代码将Excel工作表另存为制表符分隔的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆