export excel columns into multiple text files

2019-08-23 08:00发布

Input

Output

I need to split data [1...M] gathered in columns [A...N] into separate text files named as first cell of each column.

Data arranged in excel:

FileName_A  |FileName_B |FileName_C |…      |FileName_N
Data_A1     |Data_B1        |Data_C1        |…      |Data_N1
Data_A2     |Data_B2        |Data_C2        |…      |Data_N2
Data_A3     |Data_B3        |Data_C3        |…      |Data_N3
…       …       …       …       …
DataA_AM    DataA_BM    DataA_CM    DataA_AM    DataA_NM
____________________________________________________________________________

shall be written into FileNames

FileName_A.tex should look like:

_____________
Data_A1
Data_A2
Data_A3
…
DataA_AM
____________

I´ve tried, but...

Altough it seems an easy task for an expert, it is quite a huge obsticle for me becouse I am not familiar with coding.

Thank You very much for support in advance.

标签: excel vba
2条回答
Animai°情兽
2楼-- · 2019-08-23 08:30

Try Code below.

    Sub export_data()
    Dim row, column, i, j As Integer
    Dim fullPath, myFile As String

    fullPath = "C:\Workspace"
    row = 21
    column = 5

    For i = 1 To column
        myFile = Cells(1, i).Value + ".txt"
        myFile = fullPath + "/" + myFile
        Open myFile For Output As #1
        For j = 2 To row
            Print #1, Cells(j, i).Value
        Next j
        Close #1
    Next i

    End Sub

You can change Row number and Column number. Your First row is always Header. See Image below for Excel

Excel Image

查看更多
唯我独甜
3楼-- · 2019-08-23 08:51

So, I assumed that M is an Integer Variable that you already defined and N just the Column Name (So column number 14). The code would then be

Dim i As Integer
Dim LastRow As Integer

For i = 1 To 14

Range(Cells("2", i), Cells(M, i)).ExportAsFixedFormat _
    Type:=xlTypeXPS, _
    Filename:=Sheets(1).Cells("1", i), _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

Next

You might want to replace Filename:=Sheets(1).Cells("1", i), _ with Filename:="C:/Your_Path/ & Sheets(1).Cells("1", i), _

查看更多
登录 后发表回答