【发布时间】:2019-07-09 05:03:42
【问题描述】:
我可以将值从 excel 打印到记事本,但格式有点不同,
Dim txtFile As String, rng As Range, cellValue As Variant, r As Integer, c As Integer
txtFile = slocation & "\" & "Cont_name_" & Filename & ".txt"
lrow = Range("I" & Rows.Count).End(xlUp).Row
Range("A2:G" & lrow).Select
Set rng = Selection
Open txtFile For Output As #1
For r = 1 To rng.Rows.Count
For c = 1 To rng.Columns.Count
cellValue = rng.Cells(r, c).Value
If InStr(cellValue, "/") Then
cellValue = Format(cellValue, "yyyyMMDD")
End If
If c = rng.Columns.Count Then
Print #1, cellValue
Else
Print #1, cellValue,
End If
Next c
Next r
Close #1
【问题讨论】:
-
单元格中是否有尾随空格?通常要使用
Print生成固定宽度的列,您会执行类似Print #1, Spc(50 - Len(cellValue)), cellValue的操作,其中50 是列宽,cellValue之后是Trim'd。如果输出循环更容易,您也可以使用字符位置。文档是here。 -
没有尾随空格,
-
看看Fixed Field是不是你要找的?
-
请在您想要的手动输出下方添加一个比例,1xxx5xxxx0xxxx5xxxx0xxxx5xxxx0xxxx5xxxx0xxxx5 ... 这样我就可以知道所需的间距。我无法将 jpg 转换为 txt 文件。