简体   繁体   English

VBA-用于将数据从Excel导出到文本(带有分隔符的逐个单元格)

[英]VBA - For exporting data from Excel to text (cell by cell with separators)

I have a question with VBA. 我对VBA有疑问。 I'm trying to create macro for exporting my Excel sheet to txt file. 我正在尝试创建用于将我的Excel工作表导出到txt文件的宏。 > I have a sheet where I have some records and I would like to get them one by one in new line with name of column as separator. >我有一张工作表,其中有一些记录,我想在新行中以列名作为分隔符的方式逐行获取它们。 It should be like that : 应该是这样的:

"column name1" 

 1value_col1 

"coumn name2"

1value_col2

"column name 1"

2value_col1

"column name2"

2value_col2

And etc. till the end of document. 等等,直到文档末尾。

Thank you for any advice examples of something similar. 感谢您提供类似建议的任何建议示例。

Make another worksheet where you format the data as shown in your example eg 制作另一个工作表,如示例所示,在其中格式化数据

=Data!A$1
=OFFSET(Data!A1;ROUNDDOWN(ROW()/(no_of_columns*2);0);0)
=Data!A$2
...

Or if you want a macro: 或者,如果您想要一个宏:

Dim c As Long
For i = 2 To Range("A1").End(xlDown).Row
   For j = 1 To Range("A1").End(xlToRight).Column
      Worksheets("Out").Range("A1").Offset(c).Value = Cells(1, j)
      c = c + 1
      Worksheets("Out").Range("A1").Offset(c).Value = Cells(i, j)
      c = c + 1
   Next j
Next i

Then then simply save the worksheet as txt: 然后,只需将工作表另存为txt:

ActiveWorkbook.SaveAs Filename:= "C:\Book1.txt", FileFormat:=xlTextMSDOS,CreateBackup:=False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM