Excel で書式設定されたテキスト (.prn) は、1 行あたり 240 文字に制限されています

現象

Microsoft Excel では、ワークシートを書式設定されたテキスト (スペース区切り) (.prn) ファイルとして保存すると、200 文字を超える任意の文字が次の行にラップされます。

注:

同じシート上の複数の行に 240 文字を超えるテキストが含まれている場合、テキストを含む最後の行の後の行でテキストの折り返しが開始されます。

たとえば、次のシートを考えてみましょう。

Cell 文字数
A1 40
A2 255
A3 10
A4 21
A5 2
A6 52
A7 255
A8 5
A9 3
A20 13

結果の書式設定されたテキスト ファイルには、次の文字数の行が含まれます。

行番号 文字
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) ファイルの 1 行あたり 240 文字の制限があるために発生します。

回避策

Microsoft は、例示のみを目的としてプログラミング例を提供しており、明示または黙示にかかわらず、いかなる責任も負わないものとします。 これには、市販性または特定の目的との適合性についての黙示の保証も含まれますが、これに限定はされません。 この記事は、説明されているプログラミング言語、手順を作成およびデバッグするために使用されているツールに読者が精通していることを前提にしています。 マイクロソフト サポート窓口では、特定のプロシージャの機能説明に関するご質問に対して支援いたしますが、本例を特定の目的を満たすために機能を追加したり、プロシージャを構築することは行いません。 1 行あたり 240 文字の制限を超えるスペース区切りのテキスト ファイルを作成するには、次のサンプル マクロのようなマクロを使用します。

注:

このマクロを実行する前に、次の操作を行います。

  • テキスト ファイルに含めるセルを選択します。
  • 列の幅が、各列の最大の文字列を表示するのに十分な幅であることを確認します。 1 つの 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

注:

このルーチンが作成する出力ファイルは、定義上、書式設定された Text(*.prn) ファイルとは異なります。 定義上、書式設定されたテキスト ファイルには、1 行あたり 240 文字を超えることはできません。 さらに、書式設定されたテキスト ファイルには、プリンターのフォント情報も含まれています。 このサンプル マクロでは実行されません。 このソリューションは、テキスト ファイルにエクスポートするときに柔軟性を提供するように設計されています。