我有一个Excel文件,如下所示:
http://i.stack.imgur.com/AMi3c.jpg
(很抱歉没有嵌入图片,但我现在不能这样做,因为我刚刚在这里注册)
我想把它导出到一个TXT文件中,其中给出了每列的字符数。
例如:在我的文本文件中,A列的每个单元格应该恰好有3个字符,B列应该有5个字符。如果我的单元格不包含确切的字符数,那么TXT文件应该用空格填充。
因此,TXT文件应该如下所示:
http://i.stack.imgur.com/5U7AM.jpg
我以前使用过Excel Macros,但我真的不确定如何开始使用它。谢谢你的帮助,非常感谢
发布于 2016-06-03 06:19:36
这是为我工作的代码:
Sub Export_File()
Dim myFile As String, r As Integer, mypath As String, dline As String
mypath = "C:\RESULTS\" 'path for file
myFile = "file.txt" 'file name
dline = ""
For r = 2 To Range("A" & Rows.Count).End(xlUp).Row
A = Cells(r, "A") & Application.WorksheetFunction.Rept(" ", 3 - Len(Cells(r, "A")))
B = Cells(r, "B") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "B")))
C = Cells(r, "C") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "C")))
D = Cells(r, "D") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "D")))
E = Cells(r, "E") & Application.WorksheetFunction.Rept(" ", 7 - Len(Cells(r, "E")))
F = Cells(r, "F") & Application.WorksheetFunction.Rept(" ", 7 - Len(Cells(r, "F")))
G = Cells(r, "G") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "G")))
H = Cells(r, "H") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "H")))
I = Cells(r, "I") & Application.WorksheetFunction.Rept(" ", 5 - Len(Cells(r, "I")))
dline = dline & A & B & C & D & E & F & G & H & I & vbLf & vbCr
Next r
Open mypath & myFile For Output As #1 'Replaces existing file
Print #1, dline
Close #1
MsgBox ("File Created " & mypath & myFile)
End Sub
https://stackoverflow.com/questions/37596959
复制相似问题