2015-01-17 15 views
1

"Wrap Text"기능이 적용된 셀에서 줄 바꿈을 찾을 수 있습니까?"Wrap Text"기능이 적용된 셀에서 줄 바꿈을 찾을 수 있습니까?

Alt + Enter를 사용하여 새 줄을 수동으로 추가하는 텍스트에서 줄 바꿈을 찾을 수 있습니다. 다음 코드는 그것을

InStr(Range("A2").Text, vbCr) 

을 수행하지만 내가 모든 줄 바꿈은 줄 바꿈 기능을 자동으로 추가 된 세포를 작동하지 않습니다.

줄 바꿈 텍스트를 줄 바꿈으로 여러 셀로 분할하려고합니다. 세포. 이것은 가능한가?

답변

0

필자가 알고있는 한, 캐리지 리턴과 달리 텍스트가 줄 바꿈 된 텍스트에서 중단되는 부분에 대한 직접적인 처리는 없습니다. 따라서 간단한 알고리즘으로 코드에서 일부 공제해야합니다. 아래에서 시작해야합니다. 몇 가지주의해야 할 점은 아래의 코드는 특별히 공백을 두지 않을 것입니다. 이를 위해 추가 로직을 추가해야합니다. 또한 알고리즘의 가장 중요한 기능은 inCharsPerColumn 및 intColumnWidth입니다. 이 두 숫자는 주어진 폭의 열에 얼마나 많은 문자가 들어갈 지에 대한 비율을 설정합니다. 그것의 근사값은 다른 문자의 폭이 다르므로 intCharsPerColumn 변수를 과소 평가하는 것이 좋습니다 (예 : 18 열의 너비에 20 자 정도가 들어가면 18 또는 19자를 입력하십시오). 해당 비율로 게임하고 싶을 것입니다 :

Sub test() 

    Dim myRange As Range 
    Dim strTest As String 
    Dim strTemp As String 
    Dim intTest As Integer 
    Dim i As Integer 
    Dim intChar As Integer 
    Dim intColumnWidth As Integer 
    Dim dblRatio As Double 
    Dim intCharsPerColumn As Integer 

    'create ratio of approximate # of characters per given column width 
    intCharsPerColumn = 20 
    intColumnWidth = 18 
    dblRatio = intCharsPerColumn/intColumnWidth 

    i = 7 
    Set myRange = Me.Range("B7") 
    strTest = myRange.Value 

    'determine actual characters per line (approximate) 
    intTest = myRange.ColumnWidth 
    intChar = Fix(intTest * dblRatio) + 1 

    'cycle through text until each line is parsed 
    Do Until strTest = "" 
     'if the string is longer than characters per line 
     If Len(strTest) > intChar Then 
      'grab line of characters, and remove same characters from strTest 
      strTemp = Left(strTest, intChar) 
      strTest = Right(strTest, Len(strTest) - intChar) 
     Else 
      'grab the last characters of the string 
      strTemp = strTest 
      strTest = "" 
     End If 

     'place single line of text into current range 
     myRange.Value = strTemp 

     'increment the row 
     i = myRange.Row + 1 
     Set myRange = Me.Range("B" & i) 

    Loop 

End Sub