서식이 지정된 텍스트(.prn)는 Excel에서 줄당 240자로 제한됩니다.

증상

Microsoft Excel에서 워크시트를 서식 있는 텍스트(공백 구분)(.prn) 파일로 저장하면 200번째 40번째 문자를 초과하는 모든 문자가 다음 줄로 래핑됩니다.

참고

같은 시트의 여러 행에 240자를 초과하는 텍스트가 포함된 경우 텍스트가 포함된 마지막 행 뒤의 행에서 텍스트가 줄 바꿈되기 시작합니다.

예를 들어 다음 시트를 고려합니다.

문자 수
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) 파일에 줄당 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자를 초과할 수 없습니다. 또한 서식이 지정된 텍스트 파일에는 프린터 글꼴 정보도 포함됩니다. 이 샘플 매크로는 그렇지 않습니다. 이 솔루션은 텍스트 파일로 내보낼 때 유연성을 제공하도록 설계되었습니다.