简体   繁体   中英

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

I have a question with VBA. I'm trying to create macro for exporting my Excel sheet to txt file. > 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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