Excel VBA,保存为 .txt 时保持格式
Excel VBA, Keep Formatting when saving as a .txt
我正在将固定宽度的文本文件导入 Excel,然后将其导出。
我想弄清楚如何在保存时保持格式。
在导入时,我尝试使用 .NumberFormat,它在导入时强制格式化,但在导出时它会丢失格式。
我导入的文本文件:
我的导出例程:
Option Explicit
Sub CreateFixedWidthFile(strFile As String, ws As Worksheet, s() As Integer)
Dim i As Long, j As Long
Dim strLine As String, strCell As String
'get a freefile
Dim fNum As Long
fNum = FreeFile
'open the textfile
Open strFile For Output As fNum
'loop from first to last row
'use 2 rather than 1 to ignore header row
For i = 2 To ws.Range("a65536").End(xlUp).Row
'new line
strLine = ""
'loop through each field
For j = 0 To UBound(s)
'make sure we only take chars up to length of field (may want to
output some sort of error if it is longer than field)
strCell = Left$(ws.Cells(i, j + 1).Value, s(j))
'add on string of spaces with length equal to the
'difference in length between field length and value length
strLine = strLine & strCell & String$(s(j) - Len(strCell), Chr$(32))
Next j
'write the line to the file
Print #fNum, strLine
Next i
'close the file
Close #fNum
End Sub
调用它:
Sub CreateFile()
Dim sPath As String
sPath = Application.GetSaveAsFilename("", "Text Files,*.txt")
If LCase$(sPath) = "false" Then Exit Sub
'specify the widths of our fields
'the number of columns is the number specified in the line below +1
Dim s(5) As Integer
'starting at 0 specify the width of each column
s(0) = 10
s(1) = 2
s(2) = 9
s(3) = 10
s(4) = 1
s(5) = 20
'write to file the data from the activesheet
CreateFixedWidthFile sPath, ActiveSheet, s
End Sub
导出的文本文件(请参阅第 6 列的格式现在是小数和科学记数法):
您正在导出 值,这是您通常想要使用的值,以便保留实际的值.
如果您想要这些值的字符串表示形式在工作表上的显示形式,您需要导出单元格的文本 ,而不是他们的值。
strCell = Left$(ws.Cells(i, j + 1).Text, s(j))
请注意,这会降低数字精度,并且您现在将数字视为文本,如果这些值需要再次作为数字处理,这将引入问题。
我正在将固定宽度的文本文件导入 Excel,然后将其导出。
我想弄清楚如何在保存时保持格式。
在导入时,我尝试使用 .NumberFormat,它在导入时强制格式化,但在导出时它会丢失格式。
我导入的文本文件:
我的导出例程:
Option Explicit
Sub CreateFixedWidthFile(strFile As String, ws As Worksheet, s() As Integer)
Dim i As Long, j As Long
Dim strLine As String, strCell As String
'get a freefile
Dim fNum As Long
fNum = FreeFile
'open the textfile
Open strFile For Output As fNum
'loop from first to last row
'use 2 rather than 1 to ignore header row
For i = 2 To ws.Range("a65536").End(xlUp).Row
'new line
strLine = ""
'loop through each field
For j = 0 To UBound(s)
'make sure we only take chars up to length of field (may want to
output some sort of error if it is longer than field)
strCell = Left$(ws.Cells(i, j + 1).Value, s(j))
'add on string of spaces with length equal to the
'difference in length between field length and value length
strLine = strLine & strCell & String$(s(j) - Len(strCell), Chr$(32))
Next j
'write the line to the file
Print #fNum, strLine
Next i
'close the file
Close #fNum
End Sub
调用它:
Sub CreateFile()
Dim sPath As String
sPath = Application.GetSaveAsFilename("", "Text Files,*.txt")
If LCase$(sPath) = "false" Then Exit Sub
'specify the widths of our fields
'the number of columns is the number specified in the line below +1
Dim s(5) As Integer
'starting at 0 specify the width of each column
s(0) = 10
s(1) = 2
s(2) = 9
s(3) = 10
s(4) = 1
s(5) = 20
'write to file the data from the activesheet
CreateFixedWidthFile sPath, ActiveSheet, s
End Sub
导出的文本文件(请参阅第 6 列的格式现在是小数和科学记数法):
您正在导出 值,这是您通常想要使用的值,以便保留实际的值.
如果您想要这些值的字符串表示形式在工作表上的显示形式,您需要导出单元格的文本 ,而不是他们的值。
strCell = Left$(ws.Cells(i, j + 1).Text, s(j))
请注意,这会降低数字精度,并且您现在将数字视为文本,如果这些值需要再次作为数字处理,这将引入问题。