簡體   English   中英

將行集從 Excel 導出到 txt 文件

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

我的目標是將 99 行分成 10 行,每組包含 9 行和 2 列來制作 dat(text) 文件。

這可以通過在第 9 行中斷寫入操作然后在第 10 行恢復使用循環來實現。

我想要 Excel 中的前 10 行在一個 dat 文件中,接下來的 10 行在另一個中,依此類推。

當我運行以下代碼時,沒有任何反應。 當我調試時,它會跳過循環結構。 當我只為一個包含所有行的文件運行它時,它運行得很好。

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

會不會像以下代碼行中文件夾中的拼寫錯誤一樣簡單?

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

查看其余代碼,Folder 未設置為任何內容。

這一行還有一個拼寫錯誤:

conuter = 1

第一次通過,計數器 = 0,它在第一次通過循環時設置為 1。

我認為的另一個錯誤:

For k = 1 To k = LastRow

應該

For k = 1 To LastRow

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM