简体   繁体   English

将行集从 Excel 导出到 txt 文件

[英]Exporting sets of rows from Excel to txt files

My objective is to take 99 rows in groups of 10 containing 9 rows each and 2 columns to make dat(text) files.我的目标是将 99 行分成 10 行,每组包含 9 行和 2 列来制作 dat(text) 文件。

This can be achieved using loops by breaking the writing operation at 9th row and then resuming at 10th row.这可以通过在第 9 行中断写入操作然后在第 10 行恢复使用循环来实现。

I want the first 10 rows from Excel in one dat file, next 10 in another and so on.我想要 Excel 中的前 10 行在一个 dat 文件中,接下来的 10 行在另一个中,依此类推。

When I run the following code, nothing happens.当我运行以下代码时,没有任何反应。 When I debug, it skips the loop structure.当我调试时,它会跳过循环结构。 When I run it for just one file containing all the rows it runs perfectly.当我只为一个包含所有行的文件运行它时,它运行得很好。

0.8641  0.8654
0.6605  0.8269
0.5828  0.8269
0.9985  1.0000
0.7527  0.9423
0.6641  0.9423
1.1329  1.1346
0.8756  1.0962
0.7590  1.0769
0.9174  0.8836
0.7557  0.8443
0.5986  0.8164
0.9984  1.0000
0.8085  0.9656
0.6809  0.9443
1.0972  1.1328
0.8680  1.0902
0.7453  1.0623
0.8665  0.8714
0.6385  0.8429
0.5398  0.8143
Private Sub CommandButton1_Click()

' Variable declaration

Dim FilePath As String
Dim CellData As String
Dim Folder As String
Dim del As String
Dim LastCol As Long
Dim LastRow As Long
Dim FileNum As Integer
Dim count As Integer
Dim counter As Integer
Dim i, j, k As Integer

' Getting the last row and column from the workbook

LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

Foler = "C:\TRNSYS1832-bit\TRNSYS18\MyProjects\Main project\Chiller and ice storage data\Excel\"

del = vbTab

count = 9

conuter = 1

For k = 1 To k = LastRow

    FilePath = Folder & "Data" & counter & ".txt"        ' This is the final name example - Data1.txt

    FileNum = FreeFile ' FreeFile so that I don't have to associate it with a specific number

    Open FilePath For Output As #FileNum    ' Opening the file for writing 

    Print #FileNum, "-5" & vbTab & "0" & vbTab & "5" & " !Chilled water leaving temperature (C)"
    Print #FileNum, "35" & vbTab & "45" & vbTab & "50" & " !Cooling water entering temperature (C)"

    For i = 1 To count

        For j = 1 To LastCol

            If j = LastCol Then
                CellData = CellData & Round(Cells(i, j).Value, 4)
            Else
                CellData = CellData & Round(Cells(i, j).Value, 4) & del
            End If

        Next j

        Print #FileNum, CellData
        CellData = ""

    Next i

    Close #FileNum

    i = count + 1
    count = count + 9
    counter = counter + 1

Next k

End Sub

Could it be as simple as the misspelling in the folder in the following line of code?会不会像以下代码行中文件夹中的拼写错误一样简单?

Foler = "C:\TRNSYS1832-bit\TRNSYS18\MyProjects\Main project\Chiller and ice storage data\Excel\"

Looking at the rest of the code, Folder is not set to anything.查看其余代码,Folder 未设置为任何内容。

This line also has a spelling mistake:这一行还有一个拼写错误:

conuter = 1

The first time through, counter = 0, it gets set to 1 the first time through the loop.第一次通过,计数器 = 0,它在第一次通过循环时设置为 1。

Another error I think:我认为的另一个错误:

For k = 1 To k = LastRow

Should be应该

For k = 1 To LastRow

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

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