2016-04-10 14 views
0

私はテキストブロックを1行に50文字まで含むようにプログラムを作成しようとしていましたが、改行文字の場合、テキスト行の最後のスペースは50文字ですが)私は出力として値エラーを取得し続けます!ここでVBA改行コードのExcel値のエラー

コードは、これまでのところです:

Option Explicit 

Public Function LnBrk50(selection) As String 

    Dim bookmark As Long 
    Dim step As Byte 
    Dim length As Byte 

'finds the last instance number of a space character in the text block within 50 characters 
    length = 50 - Len(WorksheetFunction.Substitute(Left(selection, 50), " ", "")) 
'replaces the last instance of a space character with a line break character 
     selection = WorksheetFunction.IfError(WorksheetFunction.Substitute(selection, " ", vbNewLine, length), selection) 
'character number of the last line break in the text block 
    bookmark = WorksheetFunction.Find(vbNewLine, selection) 

    step = 0 
    Do Until step = 9 
'finds the last instance number of a space character in the text block within the 50 characters after the last line break 
     length = 50 + bookmark - Len(WorksheetFunction.Substitute(Left(selection, 50 + bookmark), " ", "")) 
'replaces the space character found in last line with a line break character 
     selection = WorksheetFunction.IfError(WorksheetFunction.Substitute(selection, " ", vbNewLine, length), selection) 
'adds character number of the last line break 
     bookmark = WorksheetFunction.Find(vbNewLine, selection, bookmark + 1) 
     step = step + 1 
    Loop 

End Function 

だから、誰もが、私はそれが#VALUEを生成する作るかもしれないもののエラーを見つけることができれば!単一の参照されたテキストセルで使用されたときにエラーが発生した場合、それは非常に感謝しています!

+0

いくつかのコメントを試みることができる:ないようにしてくださいVBAテキスト処理関数がうまくいく場合にワークシート関数を使用する。関数を使用していますが、値を返していません。ワークシートから呼び出される関数は、呼び出されたセルに表示される値を提供する必要があります。他のセルを変更しようとすべきではありません。 – OldUgly

+0

ああ、私はプログラムが、私は関数を使用するセルにDoループの最終出力を表示すると思った。 VBAの初心者であるため、関数自体を出力と同じにする必要があることはわかりませんでした。ご意見ありがとうございます! – RDub549

答えて

0

このスクリーンショットは、セルA1の非常に長いテキスト文字列を示しています。セルA2には、次のコードはLnBrk50背後にあるものである

=LnBrk50(A1) 

enter image description here

が含まれています。あなたの代わりにB1に変更されたテキストを移動するのA1を変更したい場合

Option Explicit 

Public Function LnBrk50(ByRef selection As Variant) As String 

    Dim tempStr As String 
    Dim iLoc As Long, iEnd As Long 

    tempStr = selection.Value 
    If Len(tempStr) > 50 Then 
     iLoc = 0 
     Do While Len(tempStr) - iLoc > 50 
      iEnd = iLoc + 50 
      If Len(tempStr) < iEnd Then iEnd = Len(tempStr) 
      If Not InStrRev(tempStr, " ", iEnd) > 0 Then 
       iLoc = iLoc + 1 
      Else 
       iLoc = InStrRev(tempStr, " ", iEnd) 
       tempStr = Left(tempStr, iLoc - 1) & vbNewLine & Right(tempStr, Len(tempStr) - iLoc) 
      End If 
     Loop 
    End If 
    LnBrk50 = tempStr 

End Function 

は、私はサブルーチンを使用して、ボタンに取り付ける代わりの機能を使用することをお勧めします。

+0

驚くばかり!ワークシート関数を使用するのではなく、スペースをチェックするために使用したDoループが大好きです! ByRefを別のシートから参照できるようにする必要があるので、次の列に移動するのは完璧です! – RDub549

0

あなたはあなたのコードについては、この関数が最速である面白いかもしれ

=LnBrk50(A1,50) 

と呼ばれます

Option Explicit 

Function LnBrk50(rng As Range, maxChar As Long) As String 

Dim strng2 As String, subStrng As String 
Dim arr As Variant 
Dim i As Long 

arr = Split(rng)  
i = 0 
Do While i < UBound(arr) 
    subStrng = arr(i) 
    Do While Len(subStrng) + 1 + Len(arr(i + 1)) <= maxChar 
     subStrng = subStrng + " " + arr(i + 1) 
     i = i + 1 
     If i = UBound(arr) Then Exit Do 
    Loop 
    strng2 = strng2 & subStrng & IIf(i < UBound(arr), vbCrLf, "") 
    i = i + 1 
Loop  
LnBrk50 = strng2 

End Function 

...