[英]export text file excel
I am trying to make a commandbutton that exports my table to a tab separated text file and add some dynamic headers to it. 我试图做一个命令按钮,将我的表导出到一个制表符分隔的文本文件,并向其中添加一些动态标题。 I want the headers to say: 1/x 2/x 3/x 4/x and so forth where x being the total amount of columns
我希望标题说:1 / x 2 / x 3 / x 4 / x,依此类推,其中x是列的总数
This is my code so far that I got from a website: 到目前为止,这是我从网站获得的代码:
Private Sub CommandButton1_Click()
Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
myFile = Application.DefaultFilePath & "\projekt.txt"
Set rng = Selection
Open myFile For Output As #1
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
cellValue = rng.Cells(i, j).Value
If j = rng.Columns.Count Then
Write #1, cellValue
Else
Write #1, cellValue,
End If
Next j
Next i
Close #1
End Sub
The problem is this depends on me marking the filled cells for it to work, also it only produces comma separated text files, I want tab separated files without "". 问题是,这取决于我标记填充的单元格使其起作用,它也仅生成逗号分隔的文本文件,我希望使用制表符分隔的文件不包含“”。 Also it doesnt produce headers.
也不会产生标题。
1/5 2/5 3/5 4/5 5/5
x(total) rows
Data Data Data Data Data
Data Data Data Data Data
Data Data Data Data Data
Data Data Data Data Data
It only outputs selected cells because in your code you have at line 4 它仅输出选定的单元格,因为在代码中,您在第4行
Set rng = Selection
If you want to export always a specific range, example A1:E100, you can change this line to, 如果您要始终导出特定范围(例如A1:E100),则可以将此行更改为,
Set rng = Range("A1:E100")
If this range include the headers, then your export will also have headers. 如果此范围包括标头,则您的导出也将包含标头。
Second problem, 第二个问题
It is comma separated because you have 它用逗号分隔是因为
Write #1, cellValue,
To make it tab demilited, change this line to 要使其标签无效,请将此行更改为
Write #1, cellValue & vbTab
this should be the complete code for the write section, vbNewline
implies the "new line" character (or vbCrLf
also can), vbTab
is the tab character 这应该是写部分的完整代码,
vbNewline
表示“换行”字符(或vbCrLf
也可以), vbTab
是制表符
If j = rng.Columns.Count Then
Write #1, cellValue & vbNewLine
Else
Write #1, cellValue & vbTab
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.