Save Excel Data to Txt File With no quotes

 

First: Add Microsoft Scripting Runtime Reference in Tools\References

Second: Code

VBA Code
 1 Public Sub SaveData(ByRef ws As Worksheet, ByVal FirstRowIndex As Long, ByVal LastRowIndex As Long, ByVal FirstColumnIndex As Long, ByVal LastColumnIndex As Long, ByVal FileName As String)
 2     'Dim ws As Worksheet
 3     'Set ws = Application.ActiveSheet
 4 
 5     Dim i As Long
 6     Dim j As Long
 7     Dim k As Long
 8     Dim m As Long
 9     Dim NewWorkBook As Workbook
10     Dim NewWorkSheet As Worksheet
11     Dim fso As FileSystemObject
12     Dim txtStream As TextStream
13         
14     Set NewWorkBook = Application.Workbooks.Add
15     Set NewWorkSheet = NewWorkBook.Sheets(1)
16     
17     Set fso = New FileSystemObject
18     Set txtStream = fso.CreateTextFile(FileName, Overwrite:=True, Unicode:=True)
19     
20     For i = FirstRowIndex To LastRowIndex
21         j = i - FirstRowIndex + 1
22         For k = FirstColumnIndex To LastColumnIndex
23             m = k - FirstColumnIndex + 1
24             NewWorkSheet.Cells(j, m).Value = ws.Cells(i, k)
25             
26             If k = LastColumnIndex Then
27                 txtStream.WriteLine ("")
28             Else
29                 If NewWorkSheet.Cells(j, m).Value <> "" Then
30                     txtStream.Write (NewWorkSheet.Cells(j, m).Value + vbTab)
31                 End If
32             End If
33         Next k
34     Next i
35    
36    txtStream.Close
37    NewWorkBook.Close
38 End Sub

posted on 2013-04-08 11:12  eleanor  阅读(125)  评论(0编辑  收藏  举报

导航