徵狀
在 Microsoft Excel 中,當您將工作表儲存為格式化文字 (空格分隔符) (.prn) 檔案時,超過第二百個第四十個字元的任何字元都會包裝到下一行。
注意事項
如果相同工作表上的數個數據列包含超過 240 個字元的文字,文字就會開始在包含文字的最後一列之後的列上換行。
例如,請考慮下列工作表:
儲存格 | Number of Characters |
---|---|
A1 | 40 |
A2 | 255 |
A3 | 10 |
A4 | 21 |
A5 | 2 |
A6 | 52 |
A7 | 255 |
A8 | 5 |
A9 | 3 |
A20 | 13 |
產生的格式化文字檔包含具有下列字元數目的行:
行號 | Characters |
---|---|
1 | 40 |
2 | 240 |
3 | 10 |
4 | 21 |
5 | 2 |
6 | 52 |
7 | 240 |
8 | 5 |
9 | 3 |
10-19 | 0 |
20 | 13 |
21 | 0 |
22 | 15 |
23-26 | 0 |
27 | 15 |
在此範例中的最後一行 (之後,第 20 行) ,換行的行號會從 1 開始。 實際上,第 21 行對應於第 1 行,第 22 行會對應至第 2 行,依此類推。
原因
之所以會發生此行為,是因為根據設計,格式化文字 (空格分隔) (.prn) 檔案每行限制為 240 個字元。
因應措施
Microsoft 提供的程式設計範例僅供說明之用,並不具任何明示或暗示的責任擔保。 這包括 (但不限於) 任何目的之適售性及適用性的暗示責任擔保。 本文假設您熟悉示範的程式設計語言,也熟悉用以建立和偵錯程序的工具。 Microsoft 技術支援工程師可以協助說明特定程序的功能,但不會修改這些範例以提供附加功能或建構程序來滿足您的特定需求。 若要建立超過每行 240 個字元限制的空格分隔文本檔,請使用類似下列範例宏的宏。
注意事項
執行此宏之前,請執行下列動作:
- 選取要包含在文字檔中的儲存格。
- 確認數據行寬度足以檢視每個數據行中的最大字串。 使用固定寬度字型格式化時,單一 Excel 數據行的最大欄寬為 255 個字元。
- 使用 [樣式] 功能表命令來格式化工作表,以使用固定寬度的字型。 例如,Courier 是固定寬度的字型。
您可以修改下列範例程式代碼,以匯出以空格以外的字元分隔的數據。 您必須先選取要匯出的數據範圍,再執行此範例宏。
Sub ExportText()
Dim delimiter As String
Dim quotes As Integer
Dim Returned As String
delimiter = " "
quotes = MsgBox("Surround Cell Information with Quotes?", vbYesNo)
' Call the WriteFile function passing the delimiter and quotes options.
Returned = WriteFile(delimiter, quotes)
' Print a message box indicating if the process was completed.
Select Case Returned
Case "Canceled"
MsgBox "The export operation was canceled."
Case "Exported"
MsgBox "The information was exported."
End Select
End Sub
'------------------------------------------------------------
Function WriteFile(delimiter As String, quotes As Integer) As String
' Dimension variables to be used in this function.
Dim CurFile As String
Dim SaveFileName
Dim CellText As String
Dim RowNum As Integer
Dim ColNum As Integer
Dim FNum As Integer
Dim TotalRows As Double
Dim TotalCols As Double
' Show Save As dialog box with the .TXT file name as the default.
' Test to see what kind of system this macro is being run on.
If Left(Application.OperatingSystem, 3) = "Win" Then
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"Text Delimited (*.txt), *.txt", , "Text Delimited Exporter")
Else
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"TEXT", , "Text Delimited Exporter")
End If
' Check to see if Cancel was clicked.
If SaveFileName = False Then
WriteFile = "Canceled"
Exit Function
End If
' Obtain the next free file number.
FNum = FreeFile()
' Open the selected file name for data output.
Open SaveFileName For Output As #FNum
' Store the total number of rows and columns to variables.
TotalRows = Selection.Rows.Count
TotalCols = Selection.Columns.Count
' Loop through every cell, from left to right and top to bottom.
For RowNum = 1 To TotalRows
For ColNum = 1 To TotalCols
With Selection.Cells(RowNum, ColNum)
Dim ColWidth as Integer
ColWidth=Application.RoundUp(.ColumnWidth, 0)
' Store the current cells contents to a variable.
Select Case .HorizontalAlignment
Case xlRight
CellText = Space(Abs(ColWidth - Len(.Text))) & .Text
Case xlCenter
CellText = Space(Abs(ColWidth - Len(.Text))/2) & .Text & _
Space(Abs(ColWidth - Len(.Text))/2)
Case Else
CellText = .Text & Space(Abs(ColWidth - Len(.Text)))
End Select
End With
' Write the contents to the file.
' With or without quotation marks around the cell information.
Select Case quotes
Case vbYes
CellText = Chr(34) & CellText & Chr(34) & delimiter
Case vbNo
CellText = CellText & delimiter
End Select
Print #FNum, CellText;
' Update the status bar with the progress.
Application.StatusBar = Format((((RowNum - 1) * TotalCols) _
+ ColNum) / (TotalRows * TotalCols), "0%") & " Completed."
' Loop to the next column.
Next ColNum
' Add a linefeed character at the end of each row.
If RowNum <> TotalRows Then Print #FNum, ""
' Loop to the next row.
Next RowNum
' Close the .prn file.
Close #FNum
' Reset the status bar.
Application.StatusBar = False
WriteFile = "Exported"
End Function
注意事項
根據定義,這個例程所建立的輸出檔案與格式化文字 (*.prn) 檔案不同。 根據定義,格式化文本檔每行不能包含超過 240 個字元。 此外,格式化文本檔也包含印表機字型資訊。 這個範例宏不會。 此解決方案的設計目的是要在匯出至文本檔時提供彈性。